Where will this struct be stored and why?

void Test()
    Console.WriteLine("Test:" + Test.Instance.a);

struct Test
    public static Test Instance  = new();
    public int a;

Will the Test.Instance object be stored on heap or on stack and why?

>Solution :

They can’t be stored on the stack as their is no stack frame associated with static data.

And according to Jon Skeet’s blog, "Every static variable is stored on the heap."

Leave a Reply