Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does unboxing occur when a class's value-type member is referenced?

Tags:

c#

.net

unboxing

I read What is boxing and unboxing and what are the trade offs? but can't understand one thing. Suppose I have a class:

class MyClass
{
    public int Value { get; set; }
}

And I want to get value within my method:

void MyFunc(MyClass cls)
{
    int i = cls.Value;
}

As a class placed in heap, I guess that Value placed in a heap too? And therefore operation

int i = cls.Value;

is unboxing? Or it's not unboxing?

like image 557
azhidkov Avatar asked Jan 10 '12 20:01

azhidkov


People also ask

What is boxing and unboxing in reference type variable?

Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object. In the following example, the integer variable i is boxed and assigned to object o .

How can generics avoid boxing and unboxing?

With Generics you avoid doing boxing/unboxing since you are dealing with the parameter type T , which is a natural parameter that the compiler will replace it with the concrete type at compilation time without doing the boxing operation at runtime.

What is the difference between unboxing and boxing?

The basic difference between Boxing and Unboxing is that Boxing is the conversion of the value type to an object type whereas, on other hands, the term Unboxing refers to the conversion of the object type to the value type.

What is unboxing in C sharp?

Unboxing In C# The process of converting a Reference Type variable into a Value Type variable is known as Unboxing. It is an explicit conversion process. Example : int num = 23; // value type is int and assigned value 23 Object Obj = num; // Boxing int i = (int)Obj; // Unboxing.


2 Answers

Stop thinking about stack and heap; that's completely the wrong way to think about it. It is emphatically not the case that "boxed" means "on the heap", and therefore anything "on the heap" must be "boxed".

Stack and heap are irrelevant. Rather, think about references and values. A value of value type is boxed when it must be treated as a reference to an object. If you need to have a reference to a value of a value type, you make a box, put the value in the box, and make a reference to the box. And there, now you have a reference to a value of value type.

Do not confuse that with making a reference to a variable of value type; that is completely different. A variable and a value are two very different things; to make a reference to a variable you use the "ref" keyword.

like image 114
Eric Lippert Avatar answered Sep 30 '22 01:09

Eric Lippert


Boxing or unboxing doesn't have anything to do with storing values on heap or stack. You should read the article "Boxing and Unboxing" from the C# Programming Guide. In your example none of these two occurs because you're assigning int to int.

like image 22
empi Avatar answered Sep 30 '22 01:09

empi