Finished Looking For Reference Types & Value Types & The Special Case of String in .NET
parent
6442df3678
commit
3894e9370f
|
@ -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()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue