The type NewType
is a value type, not a reference type. That means that IList
, whose type is List<NewType>
, holds copies of the values not references to them. That being said, your picture is not correct.
After that, If I print each of element in IList list, the result will
be 12 It's opposite than what I thought 22
This is the expected.
Here
i.val = 1;
IList.Add(i);
You add a copy of the value of i in the IList
. This copy's the value of val is 1.
Then
i.val = 2;
IList.Add(i);
You change the value of the val
by copying to it the value of 2. After this you add a copy of i
to the IList
. This copy's value of val is 2.
In order you notice that you have described in your question, the type NewType
should be a reference type. If you change the definition of NewType
to the following one:
class NewType
{
public int val;
}
you will notice thta you have described.