Whenever I Search for Boxing in c#, I come across a cliche example like following:
string s = "something";
Object o = s;
This is very simple to understand that a value type is cast into a very generic reference type at run time. Well and good. I want to talk about little more specific kind of boxing:
int i= something;
String s2 = 2
- Hoping that this example qualifies for the definition of boxing, I would like to note that it is not limited to string to String. It also applies to int to Integer.
- If I am not wrong all primitive type are primitive representation of their corresponding reference type and they are something we can call "light weight wrappers"
- Now my question is that is there ANY benefit of boxing primitive type to its corresponding reference type at run time at all?
Boxing is turning a value type into a reference type. It puts an object (reference type) box around the value type.
Primitive types do not have corresponding reference types so your question about
boxing primitive type to its corresponding reference type at run time
is not valid.int
is a C# keyword which serves as an alias of the predefined .NET framework value typeSystem.Int32
http://msdn.microsoft.com/en-us/library/ya5y69ds.aspxExample of boxing:
This is not boxing. String is a reference type and when it comes to C# then when you say String then it is a class and it has an alias name of string. Please refer this super question and answer given by Jon Skeet:- What's the difference between String and string?
Also there is no Integer in C# rather there is int and System.int32 which is a vlaue type and you dont have boxing on values types.
And last but not the least Java is not C#