Boxing and unboxing in c# with simple example

What are Boxing and Unboxing?

The conversion of the value type to the reference type is known as boxing and converting the reference type back to the value type is known as unboxing. 

Boxing and unboxing in c# with simple example

When the common language runtime (CLR) boxes a value type, it wraps the value inside a 
System.Object instance and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit.  

int i = 123; object o = I; // boxing

o = 123; i = (int)o; // unboxing

Disadvantages of boxing and unboxing in C#

The disadvantage of using boxing is that the same object appears at two different places in memory which can have contradictory states.

What is the difference between ref & out parameters?

An argument passed as ref must be initialized before passing to the method whereas out parameter needs not to be initialized before passing to a method.

What is the difference between Array and Array list?

In an array, we can have items of the same type only. The size of the array is fixed. An array list is similar to an array but it doesn’t have a fixed size.

What is the difference between an EXE and a DLL?

Dll is an In-Process Component whereas EXE is an OUT-Process Component.Exe is for single use whereas you can use Dll for multiple uses.
Exe can be started as a standalone where dll cannot be.

Types of comments in C#?

* Single line comments:
// for single-line comments

* Multiple line comments:
/* for multi-line comments */

* XML tags comments:
/// XML tags displayed in a code comment

What is serialization?

When we want to transport an object through a network then we have to convert the object into a stream of bytes. The process of converting an object into a stream of bytes is called Serialization. For an object to be serializable, it should inherit ISerialize Interface.
De-serialization is the reverse process of creating an object from a stream of bytes.

How does object pooling and connection pooling differ?

In Object pooling, you can control the number of connections. In connection pooling, you can control the maximum number reached. When using connection pooling, if there is nothing in the pool, a connection is created since the creation is on the same thread. In object pooling, the pool decides the creation of an object depending on whether the maximum is reached which in case if it is, the next available object is returned. However, this could increase the time complexity if the object is heavy.

What is Anonymous Method?

Anonymous methods provide a technique to pass a code block as a delegate parameter. Anonymous methods are basically methods without a name, just the body.
it is also called the inline method

delegate void NumberChanger(int n);
namespace DelegateAppl
class TestDelegate
static int num = 10;
public static void AddNum(int p)
num += p;
Console.WriteLine("Named Method: {0}", num);

public static void MultNum(int q)
num *= q;
Console.WriteLine("Named Method: {0}", num);
public static int getNum()
return num;
static void Main(string[] args)
NumberChanger nc = delegate(int x)
Console.WriteLine("Anonymous Method: {0}", x);
nc =  new NumberChanger(AddNum);
nc =  new NumberChanger(MultNum);

Anonymous Method: 10
Named Method: 15
Named Method: 30

Post a Comment

Post a Comment (0)

Previous Post Next Post