-1

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?
Lost
  • 8,195
  • 27
  • 95
  • 163
  • 1
    That's not boxing at all. String is a reference type, only value types can be boxed. String already lives in heap. – Sriram Sakthivel Aug 10 '14 at 17:03
  • do you know what the string keyword is alias for?...hint,it starts with capital S... – terrybozzio Aug 10 '14 at 17:03
  • First of all: Java and C#, big difference. `string` is a reference type in both of them so there is no boxing. What C# has is called an alias. – Jeroen Vannevel Aug 10 '14 at 17:03
  • `Integer` doesn’t exist in C#, and `System.Int32` is a value type, so no boxing. – Ry- Aug 10 '14 at 17:06
  • Your question seems to be about Java. C# doesn’t have any primitive wrappers at all. Boxing would occur by placing a value type in an `object`. – Ry- Aug 10 '14 at 17:08
  • `Object o = s` it's not boxing but rather upcasting since `object` is root for all type. – Rahul Aug 10 '14 at 17:09

2 Answers2

3
string s= something;
String s2 = 2

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#

Community
  • 1
  • 1
Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
  • C# has boxing and unboxing, please see the official documentation: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing – tscissors Dec 01 '19 at 16:08
1

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 type System.Int32 http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx

Example of boxing:

int i = 5;
object o = i;   //box the primitive type int in an object.
edtheprogrammerguy
  • 5,683
  • 5
  • 26
  • 45