C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Boxing and UnBoxing in C#


Posted Date: 04 May 2006    Resource Type: Articles    Category: .NET Framework

Posted By: Rajendra kumar Yerra       Member Level: Gold
Rating:     Points: 10



Introduction



In this article I explained about Boxing and UnBoxing Concept in c#, which is very important. In C# we have Value Types and Reference Types. Value Types are stored in stack and Reference Types are stored in Heap.

Boxing and Unboxing concept related with Value Types and Reference Types.
Boxing and unboxing enable value types to be treated as objects.

Converting a Value Type to Reference Type is called Boxing..
converting Reference Type back to the Value Type( the reverse operation of Boxing) is called as UnBoxing which is explicit.

Value Types



Value types are primitive types. All value types are stored in stack and are derived from System.ValueType. All structure and enum types that are derived from System.ValueType are created in stack, so they are called as Value Types.

Reference Types



For Reference Types memory is allocated in heap. All the classes are of reference types . ‘new’ operator in c# returns the memory address to the object.

Boxing



Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap

Let us look a small example

Example1:
// Boxing sample
int x = 22;
Object y = (Object)x;
Console.Write(y);

In Boxing there are two types

1. Explicit Boxing
2. Implicit Boxing

The difference between Implicit Boxing and Explicit Boxing is Compiler it self takes care of boxing. You may understand if u look Example2 which contains sample coding in c#

// Implicit boxing
int x = 10;
object y = x ;

Generally we use Explicit Boxing which is shown in Example1


UnBoxing



As we now Unboxing is the reverse of Boxing. That means we have to convert Reference Type(eg: object) to value Type.

In UnBoxing also there are two types like Boxing

1. Implicit UnBoxing
2. Explicit UnBoxing

Lets now see UnBoxing an object type back to value type. Here is a simple code that unbox an object back to Int32 variable. First we need to box it so that we can unbox.

Example3

int a = 5;

// Implicit Boxing

object b = a;
//Implicit UnBoxing
a = b;

This is a simple example, we are unBoxing the reference type back to value type but,
Wait our code is showing some error” Cannot implicitly convert type 'object' to 'int'”.
So implicit UnBoxing will not work. you must explicitly convert it into value type

Example4
int a = 5;
object b = a; // Implicit Boxing
a = (int) b; // Explicit UnBoxing

Example 4 Works fine.

Note:

The Type the variable uses to Box will remain the same when UnBoxing the same variable. Otherwise you will get System.InvalidCastException

Performance



Performance is less for Boxing and unboxing. these are computationally expensive processes. When a value type is boxed, an entirely new object must be created. This can take up to 20 times longer than an assignment. When unboxing, the casting process can take four times as long as an assignment

Summary



In this article we learned Boxing and Unboxing and also what are Value Types and Reference Types.

About The Author



Mr.Rajendra Kumar Yerra is a Programmer in KST , He Completed His Engineering from JNTU, Hyderabad.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Value Type  .  UnBoxing  .  Reference Type  .  Example  .  Difference  .  Boxing  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: VS2005 problem: Visual Basic compiler is unable to recover from the following error
Previous Resource: Covariant and contravariant delegates
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

it support

Contact Us    Privacy Policy    Terms Of Use