The Pool
is a variable of type List<Item>
. Hence it holds a reference to an object of type List<Item>
or it is null
. That being said, when you read this value (actually that GetPool
does), you get a reference to this object (or null).
Let's make a bit more clear what is the main difference between a value type and a reference type. int
is a value type. Let's consider the following piece of code.
int a = 4;
int b = a;
a = 5;
What are the values of a
and b
after the above declarations and assignments have been done?
The value of a
would be 5 and the value of b
would be 4. Why? The first statement copies the literal value of 4 to the a
. The second statement copies the value of a
to b
. Hence b
's value are 4. Please, pay attention here, the a
's four is different from b
's four. (It's like you have two copies of the same book. The one copy belongs to the a
and the other to b
). After the third statement, the value of a
would be 5.
On the other hand a class is a reference type. Let's declare the following class:
public Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Let's consider now that we have the following piece of code.
var personA = new Person { FirstName = "Bob", LastName = "Barney" };
var personB = personA;
personA.FirstName = "Jack";
What would be the values of personA.FirstName
and personB.FirstName
?
They would be exactly the same, Jack
. Why?
Because in this line
var personB = personA;
we have a copy by reference (since a class is a reference type). What does it mean in terms of the book's paradigm we mentioned above ? Now it is like we have thrown the physical copies of a book and we have shared a URL, in which we can read the book. Since, we both have the same reference, when something changes in the page 30, it would be visible from both of us.