This is a theoretical question and I want to test my understanding of object allocation in memory. We know that objects are allocated on the managed heap while the value types are allocated on stack memory, reason being the CLR will dynamically create the required object memory on managed heap, while the value types being of fixed length can be maintained on a stack. And the object references are also stored on stack.
Now consider the scenario here:
public class MyClass
{
public int t;
public string str;
}
public class Program {
public static void Main()
{
int a = 5;
MyClass obj = new MyClass();
obj.t = 7;
obj.str = "any string";
Console.WriteLine(obj.str);
}
}
My questions are the following:
- In this scenario, how will the stack and heap look like ?
- The class
MyClass
contains some value type, so where will these be allocated ? - The value types for
Main
are also within the classProgram
, so where will these be allocated ?
No comments:
Post a Comment