Read this
list-passed-by-ref-help-me-explain-this-behaviour[
^]
After returning from Test1 lst will still contain "Fred" and "Bob".
After returning from Test2 lst will contain "Ted" and "Ned".
List<string> lst = new List<string>();
lst.Add("Fred");
lst.Add("Bob");
Test1(lst);
Test2(ref lst);
private void Test1(List<string> lst)
{
List<string> newLst = new List<string>();
newLst.Add("Ted");
newLst.Add("Ned");
lst = newLst;
}
private void Test2(ref List<string> lst)
{
List<string> newLst = new List<string>();
newLst.Add("Ted");
newLst.Add("Ned");
lst = newLst;
}
This problem is not noticed a lot because most people use methods like Add(); and AddRange(newLst); to add items to a List.