Login
Register
Tutorials
Forum
Career Development
Resources
Reviews
Jobs
Interview
Communities
Projects
Training
Silverlight Games
|
Bookmarks
|
New Members FAQ
|
Mentor
|
Code Converter
|
IT Companies
|
Peer Appraisal
|
Members
|
Revenue Sharing
|
Computer Jokes
|
New Posts
|
Social
|
Talk to Webmaster Tony John
Online Members
baskar
naveensanagasetti
saisagar
More...
Resources
»
.NET programming
»
General
BOXING AND UNBOXING IN C#
Posted Date:
12-Feb-2010
Category:
General
Author:
Reets
Member Level:
Gold
Points
: 8
What is boxing and Unboxing in C#..!
BOXING AND UNBOXING IN C#
Boxing
refers to the conversion of a value type into a reference type whereas
Unboxing
means converting a reference type into a value type.These are very powerful features of
C#
.With boxing,you can manipulate complex reference type in the way that you would manipulate simple value types.This plays an important role in implementing polymorphism.
The following example depicted below in order to understand the concept of boxing and unboxing clearly.
...
class BoxMe
{
int iTakeObjs(object x)
}
//iTakeObjs takes an object
//and processes it here
}
object iSpitObjs()
{
//ispitObjs does the processing
//and returns an object
}
}
...
//Implementation of code
int v;
v=5;
BoxMe up = new BoxMe();
up.TakeObjs(v);
//BookMark 1
int unBox = (int) up.iSpitObjs();
//bookmark 2
...
In the example above,a class having two methods has been defined.The First method,iTakeObjs() takes in an object as a parameter.The second method
iSiptObjs() returns an object and does not take any parameters.In the implementation part of the code,a variable called
v
has been declared,which is of int type and hence,a value type.we assign 5 to
v
and also creates a new object
up
of the class
BoxMe.
The next two lines are important.In the line commented
BookMark1,
we have called the iTakeObjs method and passed
v
as a parameter.recall that itakeObjs() takes in an Object as a parameter.An object is of reference type and we have passed
v
(a value type) as a parameter.genereally,you would be inclined to think that this would generate an error.however,it does not!!.The value type
v
gets implcitly converted into a reference type.This process of converting a value type into a reference type(either explicitly or implicitly ) is known as
Boxing.
Following up from here,in the next line marked
BookMark2
, we have created a new variable type
int
called
unBox.
We have then equated this varuiable to the
iSitObjs()
method.Recall that ispitObjs() does not take in any parameters but returns an object.But we have equated it to a variable of type
int
, which is of value type.For this reason,we have to explicitly cast the object to an
int
(the type conversion is done enclosing the desired datatype in paranthesis).This process of converting a reference type into a value type is known as
Unboxing
However Note that in the mentioned example,Unboxing required an explicit cast.The runtime performs a check to make sure that the specified value type matches the type contained in the reference type.If the check in this fails,an exception is thrown,which,if not taken care of,will terminate the application.
Did you like this resource? Share it with your friends and show your love!
Tweet
Responses to "BOXING AND UNBOXING IN C#"
Author:
macxima
17 Feb 2010
Member Level:
Gold
Points
: 1
In your Example it should be like as
//Implementation of code
int v;
v=5;
BoxMe up = new BoxMe();
up.iTakeObjs(v);
//BookMark 1
because you have difine iTakeObjs() method in the above example
Regards,
mukesh
Feedbacks
Post Comment:
Notify me by email when others post comments to this article.
Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
No HTML formatting and links to other web sites are allowed.
This is a strictly moderated site. Absolutely no spam allowed.
Name:
Sign In
to fill automatically.
Email:
(Will not be published, but
required
to validate comment)
Type the numbers and letters shown on the left.
Next Resource:
FrontPage Shortkeys By Microsoft
Previous Resource:
Analyse the new project
Return to Resources
Post New Resource
Category:
General
Post resources and
earn money
!
More Resources
FrontPage Shortkeys By Microsoft
Shortcut keys for MS Excel
Various File Extensions Used in VB.NET
Short keys used in Outlook
Speed up your speed with keyboard while working on powerpoint
Short keys for MS Excel
Popular Tags
Tag posting guidelines
Search Tags
(No tags found.)
Active Members
Today
Asheej T K
(49)
baskar
(44)
Varun Bansal
(25)
Last 7 Days
baskar
(339)
Ultimaterengan
(292)
Asheej T K
(195)
more...
Awards & Gifts
Email subscription
.NET Jobs
.NET Articles
.NET Forums
Articles Rss Feeds
Forum Rss Feeds