Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ReadOnlyModifierInCSharp
{
public class Book
{
public required string Title { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ReadOnlyModifierInCSharp
{
public struct Counter
{
public int Value { get; private set; }

public void Increment()
{
Value++;
}

public readonly string Describe()
{
return $"Counted {Value} times";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace ReadOnlyModifierInCSharp
{
public class Playlist
{
private readonly List<string> _songs = new();

public int Count => _songs.Count;

public void Add(string song)
{
_songs.Add(song);
}

public void Reset()
{
//_songs = new List<string>(); //Throws Compiler Error
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,24 @@
//person.Name = "Emily"; //Throws compiler error

person.ChangeName("Emily");
Console.WriteLine($"Changed Name: {person.Name}");
Console.WriteLine($"Changed Name: {person.Name}");

var playlist = new Playlist();
playlist.Add("Bohemian Rhapsody");
playlist.Add("Hotel California");
Console.WriteLine($"Songs: {playlist.Count}");

var tally = new Tally();
tally.Count();
tally.Count();
Console.WriteLine($"Total: {tally.Total}");

var counter = new Counter();
counter.Increment();
counter.Increment();
Console.WriteLine(counter.Describe());

var book = new Book { Title = "Dune" };
Console.WriteLine($"Title: {book.Title}");

//book.Title = "Emma"; //Throws compiler error
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ReadOnlyModifierInCSharp
{
public class Tally
{
private readonly Counter _counter;

public int Total => _counter.Value;

public void Count()
{
_counter.Increment();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,34 @@ public void GivenPerson_WhenCallingChangeName_ThenNameChanged()

Assert.Equal(expectedName, person.Name);
}
}

[Fact]
public void GivenReadonlyListField_WhenAddingSongs_ThenListChanges()
{
var playlist = new Playlist();
playlist.Add("Bohemian Rhapsody");
playlist.Add("Hotel California");

Assert.Equal(2, playlist.Count);
}

[Fact]
public void GivenReadonlyStructField_WhenCountingTwice_ThenTotalStaysZero()
{
var tally = new Tally();
tally.Count();
tally.Count();

Assert.Equal(0, tally.Total);
}

[Fact]
public void GivenCounterVariable_WhenIncrementingTwice_ThenValueIsTwo()
{
var counter = new Counter();
counter.Increment();
counter.Increment();

Assert.Equal("Counted 2 times", counter.Describe());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading