I was following along a tutorial online about C#. The tutorial talks about passing-by-reference vs passing-by-value. What I am confused about specifically is that since in C#, there is a distinction between reference types vs value types, how does that distinction affect the output of the following two functions.
In the following code snippet:
public void CanSetNameFromReference()
{
Book book1 = GetBook("Book 1");
SetName(book1, "New Name");
Assert.Equal("New Name", book1.Name);
}
private void SetName(Book book, string name)
{
book.Name = name;
}
We pass in an object of type Book into SetName and we see that SetName correctly sets the name of book1 to "New Name", even though book is being passed by reference.
On the other hand in the code snippet below, this does not seem to be the case:
public void CSharpIsPassByValue()
{
var book1 = GetBook("Book 1");
GetBookSetName(book1, "New Name");
Assert.Equal("Book 1", book1.Name);
}
private void GetBookSetName(Book book, string name)
{
book = new Book(name);
}
Why is this the case?
Finally, in the code snippet below,
public void CSharpCanPassByReference()
{
var book1 = GetBook("Book 1");
GetBookSetName(ref book1, "New Name");
Assert.Equal("New Name", book1.Name);
}
private void GetBookSetName(ref Book book, string name)
{
book = new Book(name);
}
we are passing in by reference. In this case, what is happening? How is this different from the first case?