Compare commits
25 Commits
csharp_fun
...
master
Author | SHA1 | Date | |
---|---|---|---|
5396b614a0 | |||
f014ffcea4 | |||
9ea0fbe973 | |||
0435f6592e | |||
55ce261ed8 | |||
fe71127f3a | |||
f7ef36beab | |||
2b07530bb3 | |||
4623c69c69 | |||
3a7ae11abb | |||
d47a773760 | |||
394a847465 | |||
1b64a1fa8b | |||
237b8fc391 | |||
615d267706 | |||
41ca2586dc | |||
dd47f5cf72 | |||
009cdc3919 | |||
6c7ba8624f | |||
8ce65cb61e | |||
6f9f69b55f | |||
b6387e0e8c | |||
41bf0f3824 | |||
c36b3f5caf | |||
dfdcdd1281 |
@ -1,3 +1,5 @@
|
|||||||
# pluralsight-dotnet-paths
|
# pluralsight-csharp-fundamentals
|
||||||
|
|
||||||
This is a repository for learning everything about .NET from pluralsight. Including multiple learning Paths
|
This is a repository for learning course [**C# Fundamentals**](https://app.pluralsight.com/library/courses/csharp-fundamentals-dev) from pluralsight under Path [**C# Development Fundamentals**](https://app.pluralsight.com/paths/skill/csharp)
|
||||||
|
|
||||||
|
Notes are within wiki page
|
1
gradebook/.gitignore
vendored
Normal file
1
gradebook/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*Book.txt
|
56
gradebook/gradebook.sln
Normal file
56
gradebook/gradebook.sln
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio 15
|
||||||
|
VisualStudioVersion = 15.0.26124.0
|
||||||
|
MinimumVisualStudioVersion = 15.0.26124.0
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{41D689B9-3B52-432B-97EF-63FB665C1AA7}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GradeBook", "src\GradeBook\GradeBook.csproj", "{B67A3D2F-4755-46E6-960C-8746BADB002C}"
|
||||||
|
EndProject
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C32AB033-6E6D-4451-9194-0F6933BC383D}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GradeBook.Tests", "test\GradeBook.Tests\GradeBook.Tests.csproj", "{203083CF-9050-48AC-87CC-53AF6F8F79BE}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(NestedProjects) = preSolution
|
||||||
|
{B67A3D2F-4755-46E6-960C-8746BADB002C} = {41D689B9-3B52-432B-97EF-63FB665C1AA7}
|
||||||
|
{203083CF-9050-48AC-87CC-53AF6F8F79BE} = {C32AB033-6E6D-4451-9194-0F6933BC383D}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
@ -1,42 +1,148 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace GradeBook
|
namespace GradeBook
|
||||||
{
|
{
|
||||||
class Book
|
public delegate void GradeAddedDelegate(object sender, EventArgs args);
|
||||||
|
|
||||||
|
public class NamedObject
|
||||||
|
{
|
||||||
|
|
||||||
|
public NamedObject(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IBook
|
||||||
|
{
|
||||||
|
void AddGrade(double grade);
|
||||||
|
Statistics GetStatistics();
|
||||||
|
string Name { get; }
|
||||||
|
event GradeAddedDelegate GradeAdded;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class Book : NamedObject, IBook
|
||||||
|
{
|
||||||
|
// Abstract class used to act as entrypoint for other class, so output
|
||||||
|
// can change based on given input. At this level, we cannot provide implementation
|
||||||
|
// Let derived class to determine details.
|
||||||
|
|
||||||
|
public Book(string name) : base(name)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
public abstract void AddGrade(double grade);
|
||||||
|
|
||||||
|
public abstract event GradeAddedDelegate GradeAdded;
|
||||||
|
public abstract Statistics GetStatistics();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DiskBook : Book
|
||||||
|
{
|
||||||
|
public DiskBook(string name) : base(name)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override event GradeAddedDelegate GradeAdded;
|
||||||
|
|
||||||
|
public override void AddGrade(double grade)
|
||||||
|
{
|
||||||
|
using(var writer = File.AppendText($"{Name}.txt"))
|
||||||
|
{
|
||||||
|
writer.WriteLine(grade);
|
||||||
|
if (GradeAdded != null)
|
||||||
|
{
|
||||||
|
GradeAdded(this, new EventArgs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Statistics GetStatistics()
|
||||||
|
{
|
||||||
|
var result = new Statistics();
|
||||||
|
using (var reader = File.OpenText($"{Name}.txt"))
|
||||||
|
{
|
||||||
|
var line = reader.ReadLine();
|
||||||
|
while(line != null)
|
||||||
|
{
|
||||||
|
var number = double.Parse(line);
|
||||||
|
result.Add(number);
|
||||||
|
line = reader.ReadLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class InMemoryBook : Book, IBook
|
||||||
{
|
{
|
||||||
private List<double> grades;
|
private List<double> grades;
|
||||||
|
|
||||||
|
readonly string category;
|
||||||
|
|
||||||
private string name;
|
private string name;
|
||||||
|
|
||||||
public Book(string name)
|
public InMemoryBook(string name) : base(name)
|
||||||
{
|
{
|
||||||
this.name = name;
|
Name = name;
|
||||||
grades = new List<double>();
|
grades = new List<double>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddGrade(double grade)
|
public override void AddGrade(double grade)
|
||||||
{
|
{
|
||||||
this.grades.Add(grade);
|
if (grade <= 100 && grade >= 0)
|
||||||
|
{
|
||||||
|
this.grades.Add(grade);
|
||||||
|
|
||||||
|
if (GradeAdded != null)
|
||||||
|
{
|
||||||
|
// Somebody is listening/subscribing
|
||||||
|
GradeAdded(this, new EventArgs()); // sender = this, pass; Raising event
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Invalid {nameof(grade)}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ShowStaticstics()
|
public void AddGrade(char letter)
|
||||||
{
|
{
|
||||||
|
switch (letter)
|
||||||
double result = 0;
|
|
||||||
var highGrade = double.MinValue;
|
|
||||||
var lowGrade = double.MaxValue;
|
|
||||||
|
|
||||||
foreach (var number in this.grades)
|
|
||||||
{
|
{
|
||||||
highGrade = Math.Max(number, highGrade);
|
case 'A':
|
||||||
lowGrade = Math.Min(number, lowGrade);
|
AddGrade(90);
|
||||||
result += number;
|
break;
|
||||||
|
case 'B':
|
||||||
|
AddGrade(80);
|
||||||
|
break;
|
||||||
|
case 'C':
|
||||||
|
AddGrade(70);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
AddGrade(0);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
result /= grades.Count;
|
}
|
||||||
|
|
||||||
Console.WriteLine($"The average grade is {result:N1}");
|
public override event GradeAddedDelegate GradeAdded;
|
||||||
Console.WriteLine($"The highest grade is {highGrade:N1}");
|
|
||||||
Console.WriteLine($"The lowest grade is {lowGrade:N1}");
|
public override Statistics GetStatistics()
|
||||||
|
{
|
||||||
|
var result = new Statistics();
|
||||||
|
|
||||||
|
foreach (var grade in this.grades)
|
||||||
|
{
|
||||||
|
result.Add(grade);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,11 +8,63 @@ namespace GradeBook
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
var book = new Book("Scott's Grade Book");
|
var book = new DiskBook("Jason's Grade Book");
|
||||||
book.AddGrade(89.1);
|
book.GradeAdded += OnGradeAdded;
|
||||||
book.AddGrade(90.5);
|
|
||||||
book.AddGrade(87.2);
|
string input;
|
||||||
book.ShowStaticstics();
|
|
||||||
|
input = EnterGrades(book);
|
||||||
|
|
||||||
|
var stats = book.GetStatistics();
|
||||||
|
|
||||||
|
Console.WriteLine($"The lowest grade is {stats.lowGrade}");
|
||||||
|
Console.WriteLine($"The highesst grade is {stats.highGrade}");
|
||||||
|
Console.WriteLine($"The average grade is {stats.Average:N1}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string EnterGrades(Book book)
|
||||||
|
{
|
||||||
|
string input;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Give input: ");
|
||||||
|
input = Console.ReadLine();
|
||||||
|
if (input == "Q" || input == "q")
|
||||||
|
{
|
||||||
|
Console.WriteLine("Receiving termination signal; Termiate program");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Received: {input}");
|
||||||
|
book.AddGrade(Convert.ToDouble(input));
|
||||||
|
// book.AddGrade('A');
|
||||||
|
}
|
||||||
|
catch (ArgumentException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
catch (FormatException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
// Will always run for all catch scenario
|
||||||
|
Console.WriteLine("Always Done");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void OnGradeAdded(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
// Main is static, so it can only call static methods, hence return type is static
|
||||||
|
Console.WriteLine("A line has added");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
54
gradebook/src/GradeBook/Statistics.cs
Normal file
54
gradebook/src/GradeBook/Statistics.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace GradeBook
|
||||||
|
{
|
||||||
|
public class Statistics
|
||||||
|
{
|
||||||
|
public double Average
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return Sum / Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public double highGrade;
|
||||||
|
public double lowGrade;
|
||||||
|
public char Letter
|
||||||
|
{
|
||||||
|
get{
|
||||||
|
switch (Average)
|
||||||
|
{
|
||||||
|
case var d when d >= 90.0:
|
||||||
|
return 'A';
|
||||||
|
case var d when d >= 80.0:
|
||||||
|
return 'B';
|
||||||
|
case var d when d >= 70.0:
|
||||||
|
return 'C';
|
||||||
|
case var d when d >= 60.0:
|
||||||
|
return 'D';
|
||||||
|
default:
|
||||||
|
return 'F';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public double Sum;
|
||||||
|
public int Count;
|
||||||
|
|
||||||
|
public void Add(double number)
|
||||||
|
{
|
||||||
|
Sum += number;
|
||||||
|
Count += 1;
|
||||||
|
|
||||||
|
lowGrade = Math.Min(number, lowGrade);
|
||||||
|
highGrade = Math.Max(number, highGrade);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Statistics()
|
||||||
|
{
|
||||||
|
Count = 0;
|
||||||
|
Sum = 0.0;
|
||||||
|
highGrade = double.MinValue;
|
||||||
|
lowGrade = double.MaxValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
gradebook/test/GradeBook.Tests/BookTests.cs
Normal file
27
gradebook/test/GradeBook.Tests/BookTests.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace GradeBook.Tests
|
||||||
|
{
|
||||||
|
public class BookTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void BookCalculateAnAverageGrade()
|
||||||
|
{
|
||||||
|
// arrange
|
||||||
|
var book = new InMemoryBook("");
|
||||||
|
book.AddGrade(89.1);
|
||||||
|
book.AddGrade(90.5);
|
||||||
|
book.AddGrade(77.3);
|
||||||
|
|
||||||
|
// act
|
||||||
|
var result = book.GetStatistics();
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.Equal(85.6, result.Average, 1);
|
||||||
|
Assert.Equal(90.5, result.highGrade);
|
||||||
|
Assert.Equal(77.3, result.lowGrade);
|
||||||
|
Assert.Equal('B', result.Letter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
gradebook/test/GradeBook.Tests/GradeBook.Tests.csproj
Normal file
26
gradebook/test/GradeBook.Tests/GradeBook.Tests.csproj
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="1.3.0">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\src\GradeBook\GradeBook.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
141
gradebook/test/GradeBook.Tests/TypeTests.cs
Normal file
141
gradebook/test/GradeBook.Tests/TypeTests.cs
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
using System;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace GradeBook.Tests
|
||||||
|
{
|
||||||
|
using GradeBook;
|
||||||
|
|
||||||
|
public delegate string WriteLogDelegate(string logMessage);
|
||||||
|
|
||||||
|
public class TypeTests
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WriteLogDelegateCanPointToMethod()
|
||||||
|
{
|
||||||
|
string msg = "test";
|
||||||
|
|
||||||
|
WriteLogDelegate log = new WriteLogDelegate(ReturnMessage); // Instantiate a delegate
|
||||||
|
log += ReturnMessage2; // Multi-cast delegate
|
||||||
|
var result = log(msg); // Invoking a delegate
|
||||||
|
Assert.Equal(result, msg);
|
||||||
|
Assert.Equal(count, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
string ReturnMessage(string message)
|
||||||
|
{
|
||||||
|
count += 1;
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
string ReturnMessage2(string message)
|
||||||
|
{
|
||||||
|
count += 1;
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ValueTypesAlsoPassByValue()
|
||||||
|
{
|
||||||
|
var x = GetInt();
|
||||||
|
Assert.Equal(3,x);
|
||||||
|
|
||||||
|
SetInt(ref x);
|
||||||
|
Assert.Equal(42, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetInt(ref int x)
|
||||||
|
{
|
||||||
|
x = 42;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetInt()
|
||||||
|
{
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void StringsBehaveLikeValueTypes()
|
||||||
|
{
|
||||||
|
string name = "Jason";
|
||||||
|
MakeUppserCase(name); // try to change String like a reference
|
||||||
|
|
||||||
|
Assert.Equal("Jason", name); // ToUpper does not work on String, although it's reference. It's copied as value
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MakeUppserCase(string arg)
|
||||||
|
{
|
||||||
|
arg.ToUpper();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CSharpCanPassByReference()
|
||||||
|
{
|
||||||
|
var book1 = GetBook("Book1");
|
||||||
|
GetBookSetName(ref book1, "New Name");
|
||||||
|
|
||||||
|
Assert.Equal(book1.Name, "New Name");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GetBookSetName(ref InMemoryBook book, string name)
|
||||||
|
{
|
||||||
|
book = new InMemoryBook(name);
|
||||||
|
book.Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CSharpIsPassByValue()
|
||||||
|
{
|
||||||
|
var book1 = GetBook("Book1");
|
||||||
|
GetBookSetName(book1, "New Name");
|
||||||
|
|
||||||
|
Assert.Equal(book1.Name, "Book1");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GetBookSetName(InMemoryBook book, string name)
|
||||||
|
{
|
||||||
|
book = new InMemoryBook(name);
|
||||||
|
book.Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CanSetNameFromReference()
|
||||||
|
{
|
||||||
|
var book1 = GetBook("Book1");
|
||||||
|
SetName(book1, "New Name");
|
||||||
|
|
||||||
|
Assert.Equal(book1.Name, "New Name");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetName(InMemoryBook book, string name)
|
||||||
|
{
|
||||||
|
book.Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetBookReturnsDifferentObjects()
|
||||||
|
{
|
||||||
|
var book1 = GetBook("Book1");
|
||||||
|
var book2 = GetBook("Book2");
|
||||||
|
|
||||||
|
Assert.Equal("Book1", book1.Name);
|
||||||
|
Assert.Equal("Book2", book2.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void TwoVarsCanReferenceSameObject()
|
||||||
|
{
|
||||||
|
var book1 = GetBook("Book1");
|
||||||
|
var book2 = book1;
|
||||||
|
|
||||||
|
Assert.Equal(book2.Name, book1.Name);
|
||||||
|
Assert.True(Object.ReferenceEquals(book1, book2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private InMemoryBook GetBook(string name)
|
||||||
|
{
|
||||||
|
return new InMemoryBook(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user