In c# there are two type of datas .one is value type and other is reference types.
Variables of different data types
string stringVar = "Hello";
int intVar = 500;
float floatVar = 20.2f;
char charVar = 'E';
bool boolVar = true;
value types include simple types such as int,float,double, Boolean or enum, struct types.
Reference type includes class,array,interface, delegate and array.
Boxing means converting from value type to reference type.
Unboxing means converting from reference type to value type.
Boxing:
- class Test
- {
- static void Main()
- {
- int i = 1;
- object o = i; // boxing
- int j = (int)o; // unboxing
- }
- }
In the above example 1 is assigned to value type variable i.
This I value is assigned to reference type o.
This is called boxing.
Object o is type casted into type int and then assigned to value type int named j.
This is unboxing.
Note when we do boxing we directly assign the value while in unboxing there is an explicit type cast is required.
No comments:
Post a Comment