Consider the following C# code:
class Box { public int Value; }
void ChangeBox(Box b) {
b.Value = 10;
}
Box myBox = new Box { Value = 5 };
ChangeBox(myBox);
Console.WriteLine(myBox.Value);What will be printed?
class Box { public int Value; } void ChangeBox(Box b) { b.Value = 10; } Box myBox = new Box { Value = 5 }; ChangeBox(myBox); Console.WriteLine(myBox.Value);
Remember that classes are reference types in C#. Changing a property inside the method affects the original object.
The method ChangeBox receives a reference to the same Box object. Changing b.Value changes the original object's Value. So, myBox.Value becomes 10.
Look at this code:
class Container { public int Number; }
void ResetContainer(Container c) {
c = new Container { Number = 0 };
}
Container cont = new Container { Number = 5 };
ResetContainer(cont);
Console.WriteLine(cont.Number);What will be printed?
class Container { public int Number; } void ResetContainer(Container c) { c = new Container { Number = 0 }; } Container cont = new Container { Number = 5 }; ResetContainer(cont); Console.WriteLine(cont.Number);
Assigning a new object to the parameter inside the method does not change the original reference outside.
The parameter c is a copy of the reference. Assigning a new object to c only changes the local copy. The original cont still points to the old object with Number = 5.
Examine this code snippet:
class Data {
public string Text;
}
void ClearText(Data d) {
d.Text = null;
}
Data data = null;
ClearText(data);
Console.WriteLine(data.Text);Why does this code throw a NullReferenceException?
class Data { public string Text; } void ClearText(Data d) { d.Text = null; } Data data = null; ClearText(data); Console.WriteLine(data.Text);
Think about what happens when you try to access a member of a null object.
The variable data is null. Passing it to ClearText means d is null. Trying to set d.Text causes a NullReferenceException because you cannot access members of a null reference.
Given this code:
class Item { public int Id; }
void ReplaceItem(??? item) {
item = new Item { Id = 99 };
}
Item myItem = new Item { Id = 1 };
ReplaceItem(myItem);
Console.WriteLine(myItem.Id);Which method signature for ReplaceItem will make myItem.Id print 99?
class Item { public int Id; } void ReplaceItem(??? item) { item = new Item { Id = 99 }; } Item myItem = new Item { Id = 1 }; ReplaceItem(myItem); Console.WriteLine(myItem.Id);
To change the original reference, you must pass the parameter by reference.
Using ref passes the reference itself by reference, so assigning a new object inside the method changes the original variable. out requires initialization inside the method and in is readonly. Passing normally copies the reference.
Analyze this code:
void AddItems(Listlist) { list.Add("A"); list = new List (); list.Add("B"); } var myList = new List (); AddItems(myList); Console.WriteLine(myList.Count);
What number will be printed?
void AddItems(List<string> list) { list.Add("A"); list = new List<string>(); list.Add("B"); } var myList = new List<string>(); AddItems(myList); Console.WriteLine(myList.Count);
Remember that assigning a new list inside the method does not affect the original list reference.
The method first adds "A" to the original list, so myList has 1 item. Then it assigns a new list to the parameter list, but this does not change myList. Adding "B" is to the new list only. So myList.Count is 1.