|Grey Wolf wrote:|
|When you talk about C# references, what are you talking about?|
In the following, What are references, where is the object stored (heap or stack) can you store the objects where you want and what are the values of x for the four objects?
I'm not sure if that was meant as a question or not but I will try and answer it anyways though I am limited with my C# knowledge so anyone more experienced please feel free to correct me.
In you example it would depend on whether or not SomeType and OtherType are value types or reference types.
All types in C# fall into four categories Value Types, Reference Types, Generic type parameters and Pointer Types.
I won't touch on the last two but will focus on Value and Reference types.
Value types comprise most of the built-in types (All numeric types, the char type, bool) and also enum's and struct's.
Whereas Reference types comprise all class's, array's, delegate's and interface types.
Value types are just like variable in C++ they hold the content.
Whereas reference types are a bit more complex. Reference types have two parts a Object and a reference to that object. By that I mean the content of a reference type is a reference to the object that contains the value.
So in the example above lets say that t1 and t2 are reference types. Then the code below would not create new objects for t3 and t4 instead it would just create a reference to the t1 and t2 objects respectively. So any changes that are made with t3 and t4 will affect t1 and t2 directly.
SomeType t3 = t1;
OtherType t4 = t2;
As for the memory question I am not to familiar with it but will try and give some information on it, though I can't provide much. But the link below has some good information on it.
|1. A Reference Type always goes on the Heap - easy enough, right? |
2. Value Types and Pointers always go where they were declared. This is a little more complex and needs a bit more understanding of how the Stack works to figure out where "things" are declared.