Reputation: 2204
Structures are value type in C# and string is reference type. I know the difference in their memory allocation and mapping, but I am not sure how C# reference types are stored inside value type. Let's say I have a string inside the following structure. If Reference type inside Structure are created On heap then how Compiler Manages Data on heap when Structure is passed as parameter to function.
Will the string be created on stack or heap?
If I pass object of Struct Point to function how does .net manages Object of PointC inside Struct. if PointC is created on heap then passing stuct to function will have the same memories as of class ?
public class PointC
{
...
...
}
public struct Point
{
PointC obj;
}
Upvotes: 0
Views: 324
Reputation: 2205
The reference will be created on the stack (or more specifically it will be where the struct will be).
The reference will have a default value (null), so no String object will be created at the struct construction.
The String object and the string buffer will be created in the heap when you allocate the string via string constructor.
Upvotes: 1