Boxing and Unboxing in .NET
page 1 of 8
Published: 07 Sep 2006
Abstract
The article deals with the basic idea of Boxing and Unboxing techniques. Though handled automatically by the Common Language Runtime, it is useful for developers to have an understanding of how these processes affect their programs.
by Sandeep Acharya
Feedback
Average Rating: 
Views (Total / Last 10 Days): 22319/ 323

Introduction

This article deals with the boxing and unboxing in Microsoft .NET. It is a fact that in our daily coding standards we usually forget to keep track of these things, but each developer should know what it means and that the proper use of this will definitely lead to a powerful application.

Before going into Boxing and Unboxing, it is really very important to understand what a primitive data type is. All the supported datatypes by the compiler of a language are known as primitive data types. These data types map directly to the library of the corresponding language. For example, in C# we are declaring an integer variable as

Listing 1

int a = new int (5);

Or in a friendlier format:

Listing 2

int a = 5

Both listing 1 and 2 are same.

The int data type is a primitive data type of C# and for that reason it maps directly to System.Int32 type. So the above two lines can be re-written as:

Listing 3

System.Int32 a = new System.Int32 (5) 
System.Int32 a = 5

View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 2 and 6 and type the answer here:

User Comments

Title: Thanks Shakuntala   
Name: Sandeep Acharya
Date: 8/14/2008 9:42:17 PM
Comment:
Shakuntala,

Thanks for your posting....
But this one seems to be an invalid casting...If it could be a valid conversion then yes it would be an unboxing.
Title: Need clarification on Ex.of Boxing and Unboxing   
Name: Shakuntala
Date: 8/14/2008 3:28:28 AM
Comment:
String is a reference type so
if we declare
string name = "Shalini";
and
int i = Int.Parse(name);(here we are converting refernce type to int type so it is unboxing)
is this correct one??
Title: Boxing and UnBoxing   
Name: Giribabu
Date: 7/11/2008 1:16:55 AM
Comment:
Everyone is using same example, can you tell/ give me real time example's so that anyone can explain to an interviewer who is asking this question. i hope he will not satisfy by simple explaining this concept as it is. I will give you a sample where we use boxing in realtime project, you just explain me about that.

example
---------
Suppose MyTestProject Entity has productID
MyTestProject obj = new MyTestProject();
Session["ProductID"] = obj.productID;

can you explain it?
Title: Boxing & UNBoxing   
Name: prasad
Date: 5/21/2008 5:36:37 AM
Comment:
kinly give me more examples about it and what is the advantages of using boxing&Unboxing.How should explain in the interview.

Note:please write the definition
Title: Boxing & UNBoxing   
Name: venkat
Date: 4/9/2008 4:01:28 AM
Comment:
give me more examples & what is the benifit
send me mail:rams.marni@gmail.com
Title: Boxing & UNBoxing   
Name: Shashi
Date: 3/23/2008 4:36:22 AM
Comment:
give me some more examples,
and tell it benefits,Where we r using..


please give me clear idea about these Article. mail me
(shashimalraj@hotmail.com)
Title: Real Boxing   
Name: Satish Dasari
Date: 3/5/2008 11:52:23 AM
Comment:
Excellent
Title: Clarity on the Basic   
Name: B Srinivasan
Date: 12/27/2007 1:11:15 PM
Comment:
Good description with the sample to explain the Boxing concept. Its worth for the beginner/new to this .NET feature
Title: Boxing & Unboxing   
Name: saleem
Date: 12/20/2007 3:41:19 PM
Comment:
-give me some more examples,
and tell it benefits.
-Why we are using boxing and unboxing. what is the use. where
we are using this concept. can u say.

please give me clear idea about these Article. mail me @ (saleem.mas@gmail.com)
Title: Good Job   
Name: Harry
Date: 12/5/2007 6:58:43 AM
Comment:
Nice example. ;-)
Keep writing.
Title: No clarity   
Name: saki
Date: 11/5/2007 4:16:11 AM
Comment:
just ok
Title: use less article   
Name: sameer
Date: 10/9/2007 8:44:05 AM
Comment:
useless article doenst make sense to me at all
Title: Keep up the good work   
Name: Rajasekhar
Date: 8/22/2007 11:34:44 AM
Comment:
Hai,

This is the correct way to explain any concept. It's really outstanding for us. Please keep writing the articles like this.

Regards - Rajasekhar.
Title: Best explanation online about this subject   
Name: Hung Dao
Date: 7/23/2007 5:53:40 PM
Comment:
I have read several online examples. All simply talks about how boxing and unboxing work but do not really show behind the scene. Keep it up your great work.
Title: nice very good examples are given But   
Name: suyog kale(suyog.kale@gmail.com)
Date: 6/8/2007 5:36:24 AM
Comment:
give me some more examples,
and tell it benefits
Title: Boxing   
Name: shiva
Date: 5/31/2007 3:24:33 AM
Comment:
nice very good examples are given
Title: boxing   
Name: velavan
Date: 4/19/2007 7:03:44 AM
Comment:
very good
Title: About boxing and unboxing   
Name: sathyanarayanan. c
Date: 3/24/2007 7:30:47 PM
Comment:
Why we are using boxing and unboxing. what is the use. where we are using this concept. can u say.
Title: Nice   
Name: Bhupender
Date: 3/22/2007 6:21:45 AM
Comment:
Its Nice, But want some more examples ...
Title: Nice   
Name: Meenu
Date: 2/19/2007 5:44:16 AM
Comment:
Its really nice..Easily understandable..
Well done..
Title: Boxing when calling methods   
Name: ad
Date: 2/9/2007 8:20:36 AM
Comment:
Suppose we have three methods:
- Boxing::CheckBoxingInt(int32)
- Boxing::CheckBoxingObject(object)
- Boxing::CheckBoxingValueType(ValueType)

Boxing (see *) is done when calling CheckBoxingObject() and CheckBoxingValueType(), but not when calling CheckBoxingInt()...

Is this correct, or?

ldarg.0
ldloc.0
call instance void Boxing::CheckBoxingInt(int32)

ldarg.0
ldloc.0
* box [mscorlib]System.Int32
call instance void Boxing::CheckBoxingObject(object)

ldarg.0
ldloc.0
* box [mscorlib]System.Int32
call instance void Boxing::CheckBoxingValueType(class [mscorlib]System.ValueType)
Title: Fine Article   
Name: sonj
Date: 2/8/2007 10:04:03 AM
Comment:
Thanks for directing the Developers
Title: Better method   
Name: prashant kumar singh
Date: 2/4/2007 9:31:19 AM
Comment:
this site is providing better method to uderstanding any topic.
Title: Very Nice   
Name: kiran
Date: 12/22/2006 2:40:58 PM
Comment:
Please try some more......Help
Title: useful article   
Name: keert
Date: 12/15/2006 5:35:52 AM
Comment:
very useful
Title: Good one   
Name: prince
Date: 10/18/2006 7:27:40 PM
Comment:
This information helps me alot,thanks
Title: I Love You Author   
Name: I Love You Author
Date: 10/11/2006 12:45:00 AM
Comment:
I Love You Author.
Title: Thanx   
Name: mansur saeed
Date: 10/10/2006 3:06:41 PM
Comment:
think you very much &carry on
Title: I love it   
Name: .NET Pro
Date: 9/13/2006 9:40:45 PM
Comment:
I love this article, good advice.
Title: Excellent   
Name: Kishor
Date: 9/13/2006 2:26:03 AM
Comment:
Really very usefull article. Keep it up
Title: Nice one   
Name: Sarith koottalakkal
Date: 9/11/2006 10:55:39 PM
Comment:
Keep it up man..
Title: well done   
Name: udit
Date: 9/9/2006 3:43:47 AM
Comment:
the information is given by u regarding boxing & unboxing
it cleare my doubt thanks.keep in touch.
Title: Good Example   
Name: Anoop
Date: 9/9/2006 3:43:17 AM
Comment:
Good Example keep it up
Title: Keep it up   
Name: Manish
Date: 9/8/2006 3:21:08 AM
Comment:
Good, Carry on with ur work
Title: Well Done   
Name: Anita Acharya
Date: 9/7/2006 11:50:20 PM
Comment:
U have chosen a new hobby in ur life.Best of luck & God Bless U.
Title: Keep it up   
Name: prashant
Date: 9/7/2006 9:14:58 AM
Comment:
Good....
Carry on with u r work.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 11/21/2008 2:57:21 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search