.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

What is Boxing and Unboxing ?

Posted By: PankajGupta     Posted Date: July 22, 2009    Points:2   Category :C#
What is Boxing and Unboxing element?

Responses
Author: Akhil Raj             
Posted Date: July 22, 2009     Points: 5   

boxing is the process of converting value types to reference and unboxing is the reverse process

Check the following article for more details..

http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx


Author: Syed Shakeer Hussain             
Posted Date: July 22, 2009     Points: 5   

Hi Pankaj
Boxing:Converting value type to Refrence type is called Boxing.
------
example:
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

UnBoxing:Converting Refrence type to Value Type is called unboxing.
---------
example:
Int32 x = 5;
object o = x; // Implicit Boxing
x = o; // Implicit UnBoxing

Author: PankajGupta             
Posted Date: July 22, 2009     Points: 5   

Thank You Friends.

Indian Search Engine Optimization Forum
http://forum.wedost.com
Author: Rama Krishna             
Posted Date: July 23, 2009     Points: 5   

Hai Punkaj,
the process of changing value types into refference types--Boxing

the process of changing refference types into value types--UnBoxing

The example

class Test
{
static void Main() {
int i = 1;
object o = i; // boxing
int j = (int) o; // unboxing
}
}

An int value can be converted to object and back again to int.
This example shows both boxing and unboxing. When a variable of a value type needs to be converted to a reference type, an object box is allocated to hold the value, and the value is copied into the box.

Unboxing is just the opposite. When an object box is cast back to its original value type, the value is copied out of the box and into the appropriate storage location.

Author: Manas Ranjan Das             
Posted Date: September 03, 2011     Points: 20   

Converting a value type to reference type is called Boxing. Unboxing is the opposite operation and is an explicit operation.

.NET provides a unified type system. All types including value types derive from the type object. It is possible to call object methods on any value, even values of primitive types such as int.


class Test
{
static void Main() {
int i = 1;
object o = i; // boxing
int j = (int) o; // unboxing
}
}





Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend