Hi Folks,
Today I am covering last part of my article on Generics. I conclude generics by talking few cool stuffs of genercis class and differences between generics and C++ template
Generic classes encapsulate operations that are not specific to any particular data type. In general the generic class are used for working with collection like list, hashtable, stacks or queue etc where the items are store or retrieve or remove regardless of the type of data.Generic class declarations follow the same rules as normal class declarations except where noted, and particularly with regard to naming, nesting and the permitted access controls. Generic class declarations may be nested inside non-generic class declarations for example class MyList<T> {}. One interesting thing about static field in generic class is A static variable in a generic class declaration is shared amongst all instances of the same closed constructed type but is not shared amongst instances of different closed constructed types. These rules apply regardless of whether the type of the static variable involves any type parameters or not for example
class C<T>
{
static int count = 0;<O:P> </O:P>
public C() {
count++;
}<O:P> </O:P>
public static int Count {
get { return count; }
}
}<O:P> </O:P>
<O:P> </O:P>
class Application
{
static void Main() {
// same closed constructed type because count is int type.
C<int> x1 = new C<int>();
Console.WriteLine(C<int>.Count); // Prints 1<O:P> </O:P>
//different closed constructed types
C<double> x2 = new C<double>();
Console.WriteLine(C<int>.Count); // Prints 1<O:P> </O:P>
// same closed constructed type because count is int type
C<int> x3 = new C<int>();
Console.WriteLine(C<int>.Count); // Prints 2
}
}<O:P> </O:P>
the other cool stuff of generic class is default keyword .In generic classes and methods, one issue that arises is how to assign a default value to a parameterized type <T> because the value can be reference type or value type .So T = null is only valid if <T> is a reference type and T = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.The following example from the MyList<T> class shows how to use the default keyword.
public class MyList <T> where T:IComparable
{
public void Add ( T item)
{
T temp = default(T);
}
}
There are lots to talk on generic class let me stop here with an assumption that lets the readers explore the other features of generic class ,method and interface .
Now I conclude generics with explaining the differences C++ Templates and C# Generic
C# Generics and C++ templates are both language provide support for parameterized types. C# generics are a simpler approach to parameterized types without the complexity of C++ templates. The following are the differences between C# generics and C++ templates
1. C# generics do not provide the same amount of flexibility as C++ templates. For example, it is not possible to call arithmetic operators in a C# generic class, although it is possible to call user defined operators
2. C# does not allow non-type template parameters, such as template C<int i> {}
3. C# does not support explicit specialization; that is, a custom implementation of a template for a specific type
4. C# does not support partial specialization: a custom implementation for a subset of the type arguments
5. C# does not allow the type parameter to be used as the base class for the generic type
6. C# does not allow type parameters to have default types
7. In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics. C++ does allow template parameters
8. C++ allows code that might not be valid for all type parameters in the template, which is then checked for the specific type used as the type parameter. C# requires code in a class to be written in such a way that it will work with any type that satisfies the constraints. For example, in C++ it is possible to write a function that uses the arithmetic operators + and - on objects of the type parameter, which will produce an error at the time of instantiation of the template with a type that does not support these operators. C# disallows this; the only language constructs allowed are those that can be deduced from the constraints.
Cheers
Anand
http://spaces.msn.com/members/anandkumar