Awanish Welcomes You

awanish tripathi: Boxing n UnBoxing(C# continue....)

Inspirational & Genius One and the Same

personality shapes your behavior

Tuesday, January 18, 2011

Boxing n UnBoxing(C# continue....)

Boxing and Un-boxing
Boxing allows value types to be implicitly treated like objects. Suppose, we have an integer variable i as
int i = 5;
when we write
i.ToString();
The Compiler implicitly creates an instance of the Object class and boxes (stores) a copy of this int value (5) in the object. It then calls the ToString() method on the instance of the Object class which has boxed the copy of the int value. It is similar to writing
int i = 5;
Object obj = i; // implicit boxing
obj.ToString();
You can see in the above code that boxing is implicitly applied in C# and you don't have to write
Object obj = (Object) i; // unnecessary explicit boxing
On the other hand, un-boxing is explicit conversion from object type to value type. The following lines show how un-boxing is implemented in C#
int i = 5;
Object obj = i; // implicit boxing
int j = (int) obj; // explicit un-boxing
Like down-casting, un-boxing can be un-safe and throw an InvalidCastException at runtime.

My_openion:Although boxing and un-boxing look very similar to up-casting and down-casting, there are some points that differentiate the two:
• Boxing and Un-boxing are the transformations between value type and object, while casting just transforms the apparent (reference) type of objects
• Value types are stored in a stack and objects are stored in a heap. Boxing takes a copy of the value type from the stack to the heap while un-boxing takes the value type back to the stack. On the other hand, casting does not physically move or operate on the object. Casting merely changes the way objects are treated in a program by altering their reference type.
Food for thought: Exercise
1. How are inheritance & polymorphism in 'C#' different from Java and C++?
2. How can we make:
• a variable so that its value can not be changed once assigned at declaration time?
• a reference to an object so that it can not reference any other object, except for the one it is made to reference at declaration time?
• a method so that it can not be overridden by any of its sub-classes?
• a class that can not be inherited by any other class?
3. Can we override the properties and apply polymorphism on these?
4. We saw that constructors are called in the order from top to bottom (parent to child class) in inheritance hierarchy. In which order are the destructors called during inheritance?
5. What are the advantages of using the protected access modifier in inheritance?
6. How is type casting different from boxing/un-boxing?
7. When I compile and run the following program:



using System;
namespace CSharpSchool
{
class Test
{
static void Main()
{
Parent theParent = new Child();
Console.WriteLine("i = {0}", theParent.i);
theParent.MyMethod();
}
}
class Parent
{
public int i = 5;
public virtual void MyMethod()
{
Console.WriteLine("I am Parent's MyMethod()");
}
}
class Child : Parent
{
public int i = 7;
public override void MyMethod()
{
Console.WriteLine("I am Child's MyMethod()");
}
}
}


The output is:
i = 5
I am Child's MyMethod() As we called both i and MyMethod() using the Parent type reference containing the Child type object, why is i of Parent and MyMethod() of Child called?

No comments:

Post a Comment