Search code examples
c#memory-managementreferenceboxingobject-type

Is an object-type variable a reference, and that's it? or can it also be a reference to a reference?


Hi there I've got a fast question ( closed-ended question ) I faced while studying boxing and unboxing concept in C#:

The variable of "object" type, which is a reference type variable, can take a copy of value type variable or of a reference type variable.

Let's consider an object type variable declared in the stack.

Firstly, it's taking a copy of a value type variable:

int a;
object b = a;

And secondly it's taking a copy of a reference type variable:

string c;
object d = c;

Now the question here is: Compared to the first object-type variable ( object b ) which is represented in the memory by a reference in the stack with the data of int-type in the heap, the second object-type variable ( object d ) would it have a reference in the stack to the data of string text in the heap or rather it would have a reference in the stack to another reference in the heap to the data of string text?

In other words does:

string X = "ABC";

equals:

object Y = "ABC";

or either:

object Z = X;

or not

.........

Thank you guys in advance.


Solution

  • Reference types are not boxed. You would be assigning the reference from one variable to another. The only copying that happens is the reference (usually implemented as a pointer). The data in the object is not copied, but remains where it is.

    You are more or less correct with regards to value types.

    I'd advise you not to think about the heap and the stack. They are implementation details. Moreover, it's not true that value types always live on the stack. Instead, you should think about it in terms of copy semantics. Value types are always copied, and reference types are not. That's all there is to it.

    EDIT: I reread your question to try to understand what you were really asking, so I can answer more clearly.

    X is a local variable of type string that contains a reference to an object of type string. Y is a local variable of type object that contains a reference to an object of type string. This is allowed because string derives from object and a variable may safely contain a reference to an object of its own type, or any type from which that type derives (either class or interface). Z is a local variable of type object which contains a reference to an object of type string. It is functionally the same as X. You simply have two variables that contain a reference to the same object. There is no additional referencing happening.

    I think you may be confused because you might be thinking that the creation of the reference happens during assignment. All assignment does is copy the reference (for reference types) and copy the value (for value types). You can only create a reference to a reference with the ref keyword in a method parameter.

    There's an added wrinkle with string literals. They are stored in a global table, one entry per unique string literal. If you assign the same string literal to two variables, they will both point to (i.e., reference) the same entry in that global table. This is different from using new twice, but is similar to initializing two int variables with the same value, semantically.