Introduction
Before we start
tampering around the code, let's understand why we need a generic type. In
most of application development we come across situations where we need a
type that can hold both value types and reference types. In this situation we
need a generic type which can do both. Before we start working on a generic type
we'll talk a little about types in .NET.
As we know there are two fundamental
categories of types
- value
types (example: structures, primitive type, enums)
- reference
types (example: Classes, interface, arrays, strings etc..,)
So when you are
using any one of the above types they are bonded to that particular type. For
example if we declare a structure it bonds to a value type and when we declare a
class it bonds to a reference type. Despite the fact that the declaration syntax is
same for both but the semantics are distinct. And
in terms of memory allocation for a value it is on the stack, whereas for reference
type it is on the managed run-time heap.
Assignment to a variable of a value type creates
a copy of the value.
For example:
using System;
public class valTyp
{
public static void Main()
{
Int32 I=10,J=0;
J=I;
I++;
Console.WriteLine( "I value: {0}, J value {1}",I,J);
}
}
Output:
D:\vstudio>vtype
I value: 11, J value 10
The assignment to
the local variable J
does not impact the local variable I
because
both local variables are of a value type (the type Integer) and each
local variable of a value type has its own storage.
But assignment to a
variable of a reference type creates a copy of the reference, not a copy of the
value being referenced. Let�s see this with an example below.
For example:
using System;
public class Cclass
{
public int val = 0;
}
public class refTyp
{
public static void Main()
{
Cclass ref1 = new Cclass();
Cclass ref2=ref1;
ref2.val = 555;
Console.WriteLine( "Before increament Ref1 value: {0}, Ref2 value {1}",ref1.val,ref2.val);
ref2.val++;
Console.WriteLine( "After increament Ref1 value: {0}, Ref2 value {1}",ref1.val,ref2.val);
}
}
Output:
D:\vstudio>vref
Before increament Ref1 value: 555, Ref2 value 555
After increament Ref1 value: 556, Ref2 value 556
In contrast with
value type the assignment to reference type variable ref2.val = 555
,
affects the object that both ref1
and ref2
reference.
From above we see
that value types and reference types are different. So to create a Generic
type we need something which is neither of them. In .NET we have the
root type Object
(alias System.Object
) which is special in that it is neither a reference type nor a value type, and may not
be instantiated. So a variable of type Object
can either contain a value type or a reference type. The question comes to mind:
Why can the Object
class can
handle both?
The answer is both
reference and value types are derived from the base class Object
. So using the concept
of boxing and unboxing we can treat any type
as an object. So what does boxing do, and where is it necessary for a value type to
behave like an object? Boxing uses wrappers which make the value type look like a
reference object (allocated on heap), and the value type�s value is copied
into it. Then this complete wrapper is marked so that the CLR knows that it
contains a value type. Similarly the reverse process is called as unboxing.
Let�s see a sample code using Object
class as Generic type:
using System;
public class Myclass
{
public string name="Hello from Myclass";
public void doSomething()
{
Console.WriteLine(name);
}
}
public class ObjApp{
public static void CheckType(object o)
{
if (o is string)
{
Console.WriteLine ("o is String");
string s = (string)o;
Console.WriteLine (s);
}
else if (o is Int32[])
{
Console.WriteLine ("o is Int32 array");
Int32[] ins=(Int32[])o;
for(Int32 i=0;i< ins.Length;i++)
Console.WriteLine (ins[i]);
}
else if (o is Myclass)
{
Console.WriteLine ("o is Myclass");
Myclass a = (Myclass)o;
a.doSomething();
}
}
public static void Main(){
Object[] objs = new Object[3];
objs[0] = "Some String";
objs[1] = new Int32[]{1, 2, 4};
objs[2] = new Myclass();
for(Int32 ind = 0; ind < 3; ind++)
{
Console.WriteLine("Array index {0} refers to {1}",ind, objs[ind]);
ObjApp.CheckType(objs[ind]);
}
}
}
Output:
Array index 0 refers to Some String
o is String
Some String
Array index 1 refers to System.Int32[]
o is Int32 array
1
2
4
Array index 2 refers to Myclass
o is Myclass
Hello from Myclass
In
the above program if you see an Object
class it can take any type and in
the CheckType
member
function we are sending the generic type (Object
). Using the is
operator we are checking the
type which is passed as a parameter to the function. Then using Boxing
we can point to the specific type and use it for further processing.
Further
reading
- .NETMore information on .NET technologies