Boxing and unboxing in c# with simple example

What is boxing and UnBoxing ?

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

Boxing:
int Val = 1;
  Object Obj = Val; 

UnBoxing:
int Val = 1;
  Object Obj = Val; //Boxing
  int i = (int)Obj; //Unboxing


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 Arraylist?

In an array, we can have items of the same type only. The size of the array is fixed. An arraylist 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 use.
Exe can be started as 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 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 inline method
example:

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(10);
nc =  new NumberChanger(AddNum);
nc(5);
nc =  new NumberChanger(MultNum);
nc(2);
Console.ReadKey();
}
}
}

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



EmoticonEmoticon