Write a C# statement where both heap and stack memory allocation is required.

Question

Write a C# statement where both heap and stack memory allocation is required.

Answer

MyClass objMyClass= new MyClass();

A pointer called objMyClass is allocated on stack and once the compiler get to know about the new keyword it allocates the meomry on heap. So the object or the variable from stack keep the reference to the heap area.