From 3894e9370fe3825233104369db85c8fe6ed24c87 Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Fri, 19 Feb 2021 01:07:25 +0000 Subject: [PATCH] Finished Looking For Reference Types & Value Types & The Special Case of String in .NET --- gradebook/test/GradeBook.Tests/TypeTests.cs | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/gradebook/test/GradeBook.Tests/TypeTests.cs b/gradebook/test/GradeBook.Tests/TypeTests.cs index be2fd45..7b63549 100644 --- a/gradebook/test/GradeBook.Tests/TypeTests.cs +++ b/gradebook/test/GradeBook.Tests/TypeTests.cs @@ -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() {