Discover the top 5 tips for understanding .NET Interop
Home | Submit an interview question | Filter by category | Filter by job function | Filter by company
 
Member Information
UserId/Email
Password
 
Forgot Password
Technology Category
.NET (1836)
Languages (142)
Database (347)
Operating System (65)
Reporting (6)
Third-Party Tools (2)
Testing (184)
OOP (85)
Web Development (44)
Design Patterns (90)
General (17)
Networking (98)
Hardware (63)
Brain Exercise (4)
Others (45)
 Our Network
.NET Heaven
C# Corner
DbTalks
Interview Corner
Longhorn Corner
Mindcracker
VB.NET Heaven
Home » .NET » .NET
What is boxing and unboxing ?
Posted by Prabhu Raja Dec 27, 2011
Viewed : 4696 times
Major Category : .NET
Minor Category : ASP.NET
Total Replies : 2
 EDITORIAL ANSWER  
No Reply Yet
 ANSWERS BY USERS  
Lekshmana Perumal M
Jan 05, 2012
Boxing

Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object.

Consider the following declaration of a value-type variable:

int i = 123;


The following statement implicitly applies the boxing operation on the variable i:

// Boxing copies the value of i into object o.
object o = i;  


The result of this statement is creating an object reference o, on the stack, that references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable


for example:(Consider follwing C# code)

class TestBoxing
{
    static void Main()
    {
        int i = 123;

        // Boxing copies the value of i into object o.
        object o = i;  

        // Change the value of i.
        i = 456;  

        // The change in i does not effect the value stored in o.
        System.Console.WriteLine("The value-type value = {0}", i);
        System.Console.WriteLine("The object-type value = {0}", o);
    }
}
/* Output:
    The value-type value = 456
    The object-type value = 123
*/

Unboxing

Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:

  • Checking the object instance to make sure that it is a boxed value of the given value type.

  • Copying the value from the instance into the value-type variable.

    int i = 123;      // a value type
    object o = i;     // boxing
    int j = (int)o;   // unboxing
Furthe Deatils Refer :Click Here.

Prabhu Raja
Dec 27, 2011
Implicit (manual) conversion of value type to reference type of a variable is
known as BOXING, for example integer to object type conversion. Conversion of Boxed type variable back to value type is called as UnBoxing.
    
1
 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Advertise with us
Current Version: 4.2011.13.2
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved