int i=5;

i.ToString();

AND

int i=5;
Object obj=i;
obj.ToString();

I see this code in a c# book but i didnt understand this code, i got the meaning of boxing that mean Boxing allows value types to be implicitly treated like objects, plz explain those 2 code samples , thnxx

The first example is unboxed use of i; with a value type of int.
The second example is boxing i (int) in to obj; a referece type of object.

The first example is unboxed use of i; with a value type of int.
The second example is boxing i (int) in to obj; a referece type of object.

but What can i do with that unboxed i,

but the given example for unboxing is

int i=5;
Object obj=i;// boxing 
int j=(int)obj;//unboxing

The code i cant understand is

int i=5;
i.ToString();

what is the result ,

The result of the ToString() call in each example is the same; a string containing the value of i in the first case or obj in the second case, both being "5".

Boxing and unboxing are only relevant when talking about value types.
Value types are those types where the value is immediately known.
Reference types, on the other hand, must be de-referenced before any value can be known; they use a pointer to point to the actual value.
Boxing changes a value type in to a reference type.
Unboxing changes a previously boxed value type back to a value type.

commented: excellent +8
commented: Good explanation +4

Thanks nick.crane I m new to c# , i m reading a c# book and try to understand, Thanks for helping me ,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.