Boxing and Unboxing

Boxing:

Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLRlic boxes a value means when CLR is converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain.

Example:
public void fnBoxing()
{
       int i=5;
       object o=i; //implicit Boxing
}

Unboxing:

Unboxing is also a process which is used to extract the value type from the object or any implemented interface type. Boxing may be done implicitly, but unboxing have to be explicit by code.

Example:
public void fnUnboxing()

   object  o=5;
   int i=(int)o;//explicit Unboxing
 }

Comments

Popular posts from this blog

.NET Design Patterns Tutorial - MVC, MVP and MVVM Design Patterns

C# Coding Standards and Naming Conventions

What is the difference between C and C++?