Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# boxing when casting to ValueType

Tags:

c#

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?

like image 636
Zaid Masud Avatar asked Sep 19 '12 10:09

Zaid Masud


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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).

like image 63
Nir Avatar answered Oct 24 '22 16:10

Nir


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?

like image 20
Adam Houldsworth Avatar answered Oct 24 '22 17:10

Adam Houldsworth