Dgan
Dgan

Reputation: 10285

Whether It is Boxing Or Unboxing?

int i = 5;

string str = i.ToString();

String str1=(String) i.ToString();

As Int 's are value Type and String 's are reference Type

so Whether It is Boxing Or Unboxing ???

EDIT: Now For Second Statement Whether It is Boxing Or Unboxing ???

Upvotes: 1

Views: 696

Answers (5)

jdphenix
jdphenix

Reputation: 15425

Your code is not an example of unboxing or boxing, but instead a method invocation of Int32.ToString() and assigning the return value to a string. The i.ToString() call doesn't assign an int to an object, but passes it to a method which returns a string. The second line with the (string) cast is superfluous and the C# compiler doesn't even emit it into IL.

For example, if you had this in a main method:

.method private hidebysig static 
    void Main (
        string[] args
    ) cil managed 
{
// Method begins at RVA 0x2050
// Code size 19 (0x13)
.maxstack 1
.entrypoint
.locals init (
    [0] int32 i
)

IL_0000: ldc.i4.5
IL_0001: stloc.0
IL_0002: ldloca.s i
IL_0004: call instance string [mscorlib]System.Int32::ToString()
IL_0009: pop
IL_000a: ldloca.s i
IL_000c: call instance string [mscorlib]System.Int32::ToString() // cast isn't here
IL_0011: pop
IL_0012: ret
} // end of method Program::Main

If you were boxing an integer:

int i = 1; 
object iBox = i; 

emits:

.locals init (
    [0] int32 i,
    [1] object o
)

IL_0000: nop
IL_0001: ldc.i4.5
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: box [mscorlib]System.Int32
IL_0009: stloc.1
IL_000a: ret

Notice the box op code. If you aren't sure if something is boxing or unboxing, you can view the IL and see if this op code is there.

If you were unboxing an integer:

int j = (int) iBox;

The process is similar for other value types, like bool or double.

Upvotes: 4

Meena
Meena

Reputation: 169

boxing is used to convert value type(except string) to object type use ToString() method for boxing unboxing is used to convert object type to reference type(string) use anydatatype(except string).parse() method

Upvotes: 0

Waqar Ahmed
Waqar Ahmed

Reputation: 5068

boxing is happened when you assign your data to object data type. since you are not doing this, there it is not boxing or unboxing.

Upvotes: 0

user342552
user342552

Reputation:

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. Unboxing extracts the value type from the object. In this case it is neither boxing nor unboxing.

Upvotes: 1

Ekramul Hoque
Ekramul Hoque

Reputation: 5123

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.

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.

C#

int i = 123;
// The following line boxes i. 
object o = i;  

The object o can then be unboxed and assigned to integer variable i:

C#

o = 123;
i = (int)o;  // unboxing

Upvotes: 0

Related Questions