Boxing And Unboxing in C#

September 18, 2008 at 4:01 pm (C#)

1. Boxing is when we convert a value type into reference type,when we are boxing we are creating reference variables which point to a new copy on the heap.

2. Unboxing is when we convert a reference type to a value type.

3. Best example of boxing and unboxing can be while we store integer variable which is a value type in a ArrayList which is a reference type.

4. And when we retrieve the value stored in ArrayList to an integer then we change a reference type to value type ie we do unboxing.

ArrayList myArrayList = new ArrayList();Int n = 7;myArrayList.Add(n);//Boxing of nn = (int)myArrayList[0];//Unboxing of myArrayList[0]  

Permalink Leave a Comment

Two uses of using keyword in C#

September 18, 2008 at 3:22 pm (C#)

There are two uses of using keyword in C#

i)Using as Directive in C# can be used for importing namespaces to your classes.

ii)Another most useful advantage is it can be used in codeblocks what is does is it calls the Dispose() method automatically to release the resources and also automatically implements the try catch blocks for you.Here it is called a using statement.So the advantage is that we dont have to explicitly write a try-catch block and our code looks tidy.

Permalink Leave a Comment