From 8ce65cb61e048bf19d2b2a9a4a70118bf2645b97 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Fri, 19 Feb 2021 00:30:31 +0000 Subject: [PATCH] Finished Passing Parameters by Reference --- gradebook/test/GradeBook.Tests/TypeTests.cs | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/gradebook/test/GradeBook.Tests/TypeTests.cs b/gradebook/test/GradeBook.Tests/TypeTests.cs index a9a406e..be2fd45 100644 --- a/gradebook/test/GradeBook.Tests/TypeTests.cs +++ b/gradebook/test/GradeBook.Tests/TypeTests.cs @@ -6,6 +6,50 @@ namespace GradeBook.Tests using GradeBook; public class TypeTests { + [Fact] + public void CSharpCanPassByReference() + { + var book1 = GetBook("Book1"); + GetBookSetName(ref book1, "New Name"); + + Assert.Equal(book1.Name, "New Name"); + } + + private void GetBookSetName(ref Book book, string name) + { + book = new Book(name); + book.Name = name; + } + + [Fact] + public void CSharpIsPassByValue() + { + var book1 = GetBook("Book1"); + GetBookSetName(book1, "New Name"); + + Assert.Equal(book1.Name, "Book1"); + } + + private void GetBookSetName(Book book, string name) + { + book = new Book(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(Book book, string name) + { + book.Name = name; + } + [Fact] public void GetBookReturnsDifferentObjects() { @@ -16,6 +60,7 @@ namespace GradeBook.Tests Assert.Equal("Book2", book2.Name); } + [Fact] public void TwoVarsCanReferenceSameObject() { var book1 = GetBook("Book1");