Finished Passing Parameters by Reference

working_with_reference_types_and_value_types
Jason Zhu 2021-02-19 00:30:31 +00:00
parent 6f9f69b55f
commit 8ce65cb61e
1 changed files with 45 additions and 0 deletions

View File

@ -6,6 +6,50 @@ namespace GradeBook.Tests
using GradeBook; using GradeBook;
public class TypeTests 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] [Fact]
public void GetBookReturnsDifferentObjects() public void GetBookReturnsDifferentObjects()
{ {
@ -16,6 +60,7 @@ namespace GradeBook.Tests
Assert.Equal("Book2", book2.Name); Assert.Equal("Book2", book2.Name);
} }
[Fact]
public void TwoVarsCanReferenceSameObject() public void TwoVarsCanReferenceSameObject()
{ {
var book1 = GetBook("Book1"); var book1 = GetBook("Book1");