Finished Looking For Reference Types & Value Types & The Special Case of String in .NET

jason.zhu 2021-02-19 01:07:25 +00:00
parent 6442df3678
commit 3894e9370f
1 changed files with 34 additions and 0 deletions

View File

@ -6,6 +6,40 @@ namespace GradeBook.Tests
using GradeBook;
public class TypeTests
{
[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()
{