5,696,576 members and growing! (16,236 online)
Email Password   helpLost your password?
Languages » C# » Beginners     Beginner

Boxing and UnBoxing in C#

By azamsharp

This article explains the concepts of Boxing and UnBoxing in C#
C#, Windows, .NET, Visual Studio, Dev

Posted: 26 Aug 2004
Updated: 26 Aug 2004
Views: 71,845
Bookmarked: 29 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
25 votes for this Article.
Popularity: 4.52 Rating: 3.23 out of 5
5 votes, 20.0%
1
5 votes, 20.0%
2
3 votes, 12.0%
3
3 votes, 12.0%
4
9 votes, 36.0%
5

Introduction

In this article I will explain the concepts of Boxing and UnBoxing. C# provides us with Value types and Reference Types. Value Types are stored on the stack and Reference types are stored on the heap. The conversion of value type to reference type is known as boxing and converting reference type back to the value type is known as unboxing.

Let me explain you little more about Value and Reference Types.

Value Types

Value types are primitive types that are mapped directly to the FCL. Like Int32 maps to System.Int32, double maps to System.double. All value types are stored on stack and all the value types are derived from System.ValueType. All structures and enumerated types that are derived from System.ValueType are created on stack, hence known as ValueType.

Reference Types

Reference Types are different from value types in such a way that memory is allocated to them from the heap. All the classes are of reference type. C# new operator returns the memory address of the object.

Examples

Lets see some examples to have a better understanding of Value Types and Reference Types. Since we know that all ValueTypes are derived from System.Value we can write something like this:

 System.ValueType r = 5;       

So what do you think about the above line of code. Will it compile ? Yes it will compile. But wait what type is it cause I don't remember any type which is called System.ValueType since its a base class from which all value types inherit. So is it Int32, Int64,double, decimal etc. It turns out that the type for variable 'r' is System.Int32. The Question arrises why Int32 and why not Int16. Well its because it is mapped to Int32 by default depending upon the Initial value of the variable.

You cannot write something like this since System.ValueType is not a primitive type its a base class for primitive value types and these mathematical operations can be performed on primitive types.

System.ValueType r = 10; 
r++;

In the above example I told you that variable 'r' will be a System.Int32 variable but if you don't believe me than you can find out yourself using the GetType() method:

 System.ValueType r = 5;
 Console.WriteLine(r.GetType()) // returns System.Int32; 

Here are few samples you can try on your own:

 
        
        System.ValueType r = 23.45; 
        Console.WriteLine(r.GetType()); // what does this print

        //-------------------------------------------------------

        System.ValueType r = 23.45F; 
        Console.WriteLine(r.GetType()); // What does this print

        //-------------------------------------------------------

        System.ValueType r = 2U; 
        Console.WriteLine(r.GetType()); // What does this print

        //-------------------------------------------------------

        System.ValueType r = 'c';
        Console.WriteLine(r.GetType()); // What does this print

        //-------------------------------------------------------

        System.ValueType r = 'ac';
        Console.WriteLine(r.GetType()); // tricky 

        //-------------------------------------------------------

        System.ValueType r = "Hello World"; 
        Console.WriteLine(r.GetType()); // tricky

        
        

Lets now jump to Boxing. Sometimes we need to convert ValueTypes to Reference Types also known as boxing. Lets see a small example below. You see in the example I wrote "implicit boxing" which means you don't need to tell the compiler that you are boxing Int32 to object because it takes care of this itself although you can always make explicit boxing as seen below right after implicit boxing.

 
        Int32 x = 10; 
        object o = x ;  // Implicit boxing

        Console.WriteLine("The Object o = {0}",o); // prints out 10

        //-----------------------------------------------------------

        Int32 x = 10; 
        object o = (object) x; // Explicit Boxing

        Console.WriteLine("The object o = {0}",o); // prints out 10

        
        

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.

 
        Int32 x = 5; 
        object o = x; // Implicit Boxing

        x = o; // Implicit UnBoxing

        

So, you see how easy it is to box and how easy it is to unbox. The above example first boxs Int32 variable to an object type and than simply unbox it to x again. All the conversions are taking place implicitly. Everything seems right in this example there is just one small problem which is that the above code is will not compile. You cannot Implicitly convert a reference type to a value type. You must explicity specify that you are unboxing as shown in the code below.

 
        Int32 x = 5; 
        object o = x; // Implicit Boxing

        x = (Int32)o; // Explicit UnBoxing

        

Lets see another small example of unboxing.

 
        Int32 x = 5; // declaring Int32

        Int64 y = 0; // declaring Int64 double

        object o = x; // Implicit Boxing

        y = (Int64)o; // Explicit boxing to double

        Console.WriteLine("y={0}",y); 
        

This example will not work. It will compile successfully but at runtime It will generate an exception of System.InvalidCastException. The reason is variable x is boxed as Int32 variable so it must be unboxed to Int32 variable. So, the type the variable uses to box will remain the same when unboxing the same variable. Of course you can cast it to Int64 after unboxing it as Int32 as follows:

 
         Int32 x = 5; // declaring Int32

        Int64 y = 0; // declaring Int64 double

        object o = x; // Implicit Boxing

        y = (Int64)(Int32)o; // Unboxing and than casting to double

        Console.WriteLine("y={0}",y); 
        

I am sure that you all have grasp the basic understanding of Boxing and Unboxing. Happy Coding and practise a lot !

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

azamsharp


I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.

HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.

ScreencastADay is a website committed to educate you everyday. The idea of the website is really simple. We believe that in order to be successful you need to learn something new everyday. ScreencastADay puts this thought into action by providing new screencast every day. This means you will get 7 screencasts a week and 365 screencasts a year unless of course it is a leap year in that case you get one more bonus screencast.

HighOnCoding.com www.HighOnCoding.com

ScreencastADay.com: www.ScreencastADay.com

RefactorCode: www.RefactorCode.com

GridViewGuy: GridViewGuy

My Blog:

Blog

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
GeneralBut Why?memberDJ_B3:17 16 Mar '08  
GeneralRe: But Why?memberjasonmdli12:20 8 Sep '08  
GeneralWhy has c# adopted boxing?memberAJ1239:19 3 Jan '06  
GeneralBoxing same as casting in C?memberLiamD22:54 29 Nov '05  
GeneralNice OnememberDandyGroove1:26 21 Jul '05  
GeneralWatch outmemberLorad11:16 20 Sep '04  
GeneralRe: Watch outsussAnonymous8:46 21 Sep '04  
GeneralErrormembereggie56:17 13 Sep '04  
GeneralRe: Errormemberazamsharp13:57 19 Sep '04  
GeneralMistakememberKinlan0:55 2 Sep '04  
GeneralRe: Mistakememberazamsharp3:25 2 Sep '04  
GeneralRe: MistakememberKinlan7:10 2 Sep '04  
GeneralMISTAKEmemberazamsharp8:13 2 Sep '04  
GeneralPerformance considerationsmemberMike.NET7:24 27 Aug '04  
GeneralRe: Performance considerationsmemberazamsharp8:58 27 Aug '04  
GeneralRe: Performance considerationsmemberJeffrey Sax12:15 6 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Aug 2004
Editor: Nishant Sivakumar
Copyright 2004 by azamsharp
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project