http://csharpforwings.blogspot.com/

Hello experts of C# plz check out this lesson and give your Precious comments

10:33, 2007-Nov-2 .. 0 comments .. Link

LESSON : Boxing & Un-Boxing

C# has a number of features; in this lesson, we are going to discuss about a special feature called “Boxing & Un-Boxing”.

 

First we’ll see about what is meant by “Boxing”

Boxing is nothing but converting a value type object to reference type object.

 

When a variable of value type needs to be converted to reference type, an object box is allocated to hold the value; and the value is copied into that box. This process of copying a value to a box is called Boxing.

 

Analogy: It’s similar to that process of transferring data from a Floppy disk to our PC .Likewise in boxing it is possible to convert a value type object to reference type object.

 

Un-Boxing:

Un-Boxing is the reverse process of Boxing i.e. converting reference type back to the value type is known as Un-Boxing.

 

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

 

For example: Similar to re – transferring the data from our PC to a Floppy disk Drive reconverting like wise un-boxing will convert reference type into value type objects.

 

Here’s a sample program which gives you a better understanding.


using System;

class Boxing

{

            static void Main()

            {

                        int a = 123;    //variable declarations

                        object box = a;   // boxing

                        Console.WriteLine("Value of Boxed Object  {0}",box);            //printing the value here

 

                        int b = (int) box;             // unboxing

                        Console.WriteLine(“Value of Un-Boxed Object {0}”,b);   //printing the converted value here

                        Console.ReadLine();

            }

}

 

In the sample program, we have used both the boxing and un-boxing features.

 

 

This line is very much familiar to us. Let’s declare a namespace “System” here.

Then we are creating a class namely “Boxing”.

In the main method, we declare a variable namely “a” with integer type. And we have also assigned the value of “a” is equal to “123”.

Now, we are going to box the value “123”. For that, we are creating an object namely “box” and we also assign the variable ‘a’ to the box object. The value of “a” is transferred to the object “box” while compiling this line.

 

Here, we can verify the content in the object box using “Write line” method.

Next, we are un-boxing the content of the object “box” and stored it in the variable “b”.

When the compiler compiles this line, object “box” content will be transferred to “b”.

To verify the content in “b” also we can use the Write line method.

Save the application with .cs extension.

Let’s save it as “boxing.cs”

 

Well, now we build our application.

In Visual Studio .NET command prompt, type file name with extension; then press enter

Let’s type the name as “boxing.cs”

 

Here’s our file successfully executed. Open “exe” file to check output.

 

This line “Value of Boxed Object” is “123” which indicates that we have boxed the value.

And “Value of Un-Boxed Object” is “123” which indicates that we have un-boxed the boxed value.

 

This lesson ends up with “Boxing & Un-Boxing”. I hope you are clear with this concept. In the next lesson, we will discuss some more interesting topics.


Leave a Comment

{ Last Page } { Page 1 of 2 } { Next Page }

About Me

Home
My Profile
Archives
Friends
My Photo Album

Links


Categories


Recent Entries

Hello experts of C# plz check out this lesson and give your Precious comments
plz help me to help others

Friends

Google