6

I was reading about the ValueType class and I am wondering if, when something gets casted to a ValueType, whether it is getting boxed? Example:

void DoSomething(ValueType valueType)
{
}

DoSomething(5);

Did the int represented by the literal 5 get boxed when received by DoSomething method?

Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
  • 3
    I think [Marc Gravell's note at your link](http://msdn.microsoft.com/en-us/library/system.valuetype%28v=vs.100%29.aspx#1) is very clear on that subject :) – Sergey Kalinichenko Sep 19 '12 at 10:45
  • Also see [boxing-occurrence-in-c-sharp](http://stackoverflow.com/questions/7995606) – nawfal Jun 03 '13 at 08:08
  • possible duplicate of [Boxing on assignment to ValueType?](http://stackoverflow.com/questions/7205556/boxing-on-assignment-to-valuetype) – colinfang Jun 26 '13 at 11:50

3 Answers3

3

Yes, it gets boxed.

Think about it... for the value not to get boxed there should be some common binary representation that can be any value type - including all built in ones and any struct you may define in the future.

Since such a binary representation doesn't exist the value must be boxed.

Explanation:

When you call a method with parameters the caller places a sequence of bits at an agreed about location and in an agreed about format, for example an int is 32bits with negative numbers encoded as 1-complement, a double is 64bits encoded in IEEE floating point format, etc.

You can't have one method that can except both unboxed int and double because it wouldn't know how many bits to read and how to decode themץ

If you do want a method to accept both you can give the function the memory location of the value (the location itself is of known size and format so the method knows how to decode it) and some meta data so the method knows the actual type of the value - wrapping the value with metadata and providing it's memory location is called (surprise, surprise) "boxing"

So, anytime you pass a value using a parameter/variable/whatever that is not the exact type the system has to box the value or the receiver wouldn't know much memory the value really uses and how to decode that memory from a sequence of bits back to a number or structure.

This only applies to value types because reference types are always passed by using the memory location (the memory location is called a "reference" in .net).

Nir
  • 29,306
  • 10
  • 67
  • 103
  • What do you mean by "common binary representation?" What is an example of a common binary representation for `int` value type? – Zaid Masud Sep 19 '12 at 15:25
  • @ZaidMasud - when you call a method with parameters the caller places a sequence of bits at an agreed about location and in an agreed about format, for example an int is 32bits with negative numbers encoded as 1-complement, a double is 64bits encoded in IEEE floating point format, etc. -- you can't have one method that can except both unboxed int and double because it wouldn't know how many bits to read and how to decode them, ... – Nir Sep 19 '12 at 19:54
  • ... if you do want a method to accept both you can give the function the memory location of the value (the location itself is of known size and format so the method knows how to decode it) and some meta data so the method knows the actual type of the value - wrapping the value with metadata and providing it's memory location is called (surprise, surprise) "boxing" -- ... – Nir Sep 19 '12 at 20:01
  • ... so, anytime you pass a value using a parameter/variable/whatever that is not the exact type the system has to box the value or the receiver wouldn't know much memory the value really uses and how to decode that memory from a sequence of bits back to a number or structure – Nir Sep 19 '12 at 20:03
  • Good explanation, I would suggest moving the explanation in your comments into the text of your answer. – Zaid Masud Sep 24 '12 at 10:08
  • This one is a great explanation of how and why boxing happens. I think this is what should be written in books and tutorials, not just "it happens when you use `object` and it causes overhead". – Yeldar Kurmangaliyev Dec 21 '18 at 16:46
2

Yes it does, ValueType is a class (and thus a reference type so will incur boxing).

This question covers similar ground:

Why Enum's HasFlag method need boxing?

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
0

According to Marc Gravell's comment in the MSDN article you link to it is.

it should be stressed that while ValueType can be used to restrict values to value-types, casting to ValueType (implicitly or explicitly) is still a boxing operation; only the concrete known value-types ("DateTime", "int", etc) can be handled directly as value-types - ValueType itself is treated as a class (so boxing).

PHeiberg
  • 29,411
  • 6
  • 59
  • 81