Obviously if I made a list and filled it with “new object()” then it would be storing the memory for the object, but what if I made a list composed of only objects that were already defined, would this list essentially be a list of handles, or would all the data / memory copy to the list?
I just want to know so I can write code to be more clean, like if it’s actually copying the memory then I’ll do things differently because I don’t want it to make memory copies, that causes lag.
Does your answer also apply to arrays?
No, it would be storing a reference to the object. The value of any expression in C# is a value type value, a reference, or a pointer. Never an actual object.
So this:
is equivalent to this:
In both cases, the list will contain a reference to the object. This isn’t just the case for lists – it’s vital you understand it for assignment, parameter passing – anything where a reference is copied, basically. So as a simple example:
See my articles on value and reference types, and parameter passing for more information.