Amit Amit - 11 months ago 51
C# Question

Data type of reference variable

Consider the following declaration:

Class1 c1=new Class1();

stack and heap

The reference variable
is kept in the stack and the actual object is allocated in the heap.

What is the type of this "reference variable"
in the stack? Is it an integer pointer pointing to memory address of the heap?

Answer Source

First off, c1 is not necessarily sitting on the stack. It's a value, and that value will sit wherever that variable is stored. It might be on the stack, it might not, there are lots of factors that influence where a given variable is stored.

As far as what the type of the variable is, the type of the variable c1 is Class1. That the value of that variable is a reference to an object that is elsewhere, rather than the data comprising a Class1 instance is what it means to have a variable of type Class1. By definition a storage location that contains a reference to an instance of type Class1 that is elsewhere is what it means to have a variable of type Class1, and that's exactly what c1 is.