Sunday, 15 September 2013

Why can I add item to my list but not reallocate it?

Why can I add item to my list but not reallocate it?

This might be a very noob question but I cannot fully understand why am I
able to add items to my list and why can't I set it to null ?
public class Test
{
public List<string> Source { get; set; }
public Test()
{ this.Source = new[] { "Hey", "hO" }.ToList(); }
}
class Program
{
static void Main(string[] args)
{
Test test = new Test();
ModifyList(test.Source);
//Why here test.Source.Count() == 3 ? Why isn't it null ?
}
private static void ModifyList(List<string> list)
{
list.Add("Three");
list = null;
}
}
Why after the call ModifyList, test.Source.Count() == 3 ? Why isn't it null ?
I would have expected the list to be either NULL or remain unchanged with
two elements.
Can someone explain to me what is happening? Thank you very much!

No comments:

Post a Comment