Discuss Boxing and Unboxing in the context of value types and reference types

 

About Boxing:

Converting a value type to reference type is called Boxing.

About UnBoxing:

Converting a reference type to value type is called UnBoxing.

Value Types

A data type is a value type if it holds the data within its own memory allocation. Value Types get allocated on stack.

Value Types: Boolean, Char, and Date, SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong, Struct

Reference Types

A reference type contains a pointer to another memory location that holds the data. Reference Types get allocated on heap.

Reference Types: Class, Objects and All arrays, even if their elements are value types.

With Boxing and Unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to any value of a reference-type and vice versa.

Simple Example of Boxing

 
       class Program
      {
          static void Main(string[] args)
	    {
		int A = 5;
	      object B = A;//Boxing
          }
	}

As you see in our above code that we have converted value type "int A" with value 5 to reference type "object B" by assigning value type to reference type object.

Simple Example of UnBoxing

      class Program
      {
          static void Main(string[] args)
	    {
		int A = 5;
	      object B = A;//Boxing
            int C = (int)B;// Unboxing
          }
	}

As you see in our above code that we have converted reference type "object B" with reference value 5 to value type "Int C" by assigning reference type object to value type.

To check with a valid proof of boxing and unboxing you can check above code i.e. compiled code "dll" or "exe" file using "ILDASM" tool.ILDASM tool which shows half partially compiled code i.e. IL code or Intermediate Language Code.

Please use Visual Studio Command Prompt to launch ILDASM tool

Command: ILDASM and Press Enter Key

Visual Studio Command Prompt : Get in visual studio tools inside your program files.

So this is all about boxing and unboxing if you have any query regarding this topic kindly let me know through your comments. if you like this article share it with your friends.

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Jul
07
2013

Understanding Switch Statement with the help of an example

CLICK TO READ FULL ARTICLE

Views - 5141 |Category - C#

Jul
07
2013

Checked and Unchecked operators using an example

CLICK TO READ FULL ARTICLE

Views - 5843 |Category - C#

Jul
07
2013

What is an Indexer? Explain how it is different from property in terms of implementation.

CLICK TO READ FULL ARTICLE

Views - 8004 |Category - C#

Jul
05
2013

What are structures? Differentiate between structures and classes

CLICK TO READ FULL ARTICLE

Views - 5512 |Category - C#

Jun
30
2013

Understanding the concept of pass by value and pass by reference with an example?

CLICK TO READ FULL ARTICLE

Views - 5869 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

9uv24

 

 
Comments

Posted by Vikrant on 2013-08-16

Hi, good article.. but there is a typing mistake.. Boolean is written in both i.e. in value type and reference type.. so remove it from reference type.