Thursday, October 4, 2007

What is Generics

Generic classes and methods combine reusability, type safety and efficiency in a way that their non-generic counterparts cannot. Generics are most commonly used with collections and the methods that operate on them. Version 2.0 of the .NET Framework class library provides a new namespace, System.Collections.Generic, which contains several new generic-based collection classes. It is recommended that all applications that target Version 2.0 use the new generic collection classes instead of the older non-generic counterparts such as ArrayList.

For moer details, see this article
http://msdn2.microsoft.com/en-us/library/512aeb7t(vs.80).aspx


C#
// Declare the generic class
public class GenericList
{
void Add(T input) { }
}
class TestGenericList
{
private class ExampleClass { }
static void Main()
{
// Declare a list of type int
GenericList list1 = new GenericList();
// Declare a list of type string
GenericList list2 = new GenericList();
// Declare a list of type ExampleClass
GenericList list3 = new GenericList();
}
}

MORE DETAILS



Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations

Remember Boxing and unboxing when come across geenrics


create your own generic interfaces, classes, methods, events and delegates

The data type of the parameters can change with each use

Sample method using Generic DataType

protected GenType MyMethod(GenType item)
{
return item;
}

Example

// This method will swap any two items.
// as specified by the type parameter .
static void Swap(ref T a, ref T b)
{
Console.WriteLine("You sent the Swap() method a {0}",
typeof(T));
T temp;
temp = a;
a = b;
b = temp;
}



Generics provide a way for programmers to define “placeholders” (formally termed type parameters) for method arguments and type definitions, which are specified at the time of invoking the generic method or creating the generic type.


By convention, generic types specify their placeholders using uppercase letters. Although any letter (or word) will do, typically T is used to represent types, K is used for keys, and V is used for values.


.NET generics may be defined with optional constraints using the where keyword to address issues like Cannot convert type T to..given that the compiler does not yet know the value of the type parameter and cannot guarantee the cast would be legal
Example:-

where T : struct The type parameter must have System.ValueType in its chain of inheritance.
where T : class The type parameter must not have System.ValueType in its chain of inheritance (e.g., must be a reference type).


// Contained items must have a default ctor.
public class MyGenericClass where T : new()
{...}
// Contained items must be a class implementing IDrawable
// and support a default ctor.
public class MyGenericClass where T : class, IDrawable, new()
{...}
// MyGenericClass derives from MyBase and implements ISomeInterface,
// while the contained items must be structures.
public class MyGenericClass : MyBase, ISomeInterface where T : struct
{...}

(http://en.csharp-online.net/Understanding_Generics%E2%80%94Constraining_Type_Parameters_Using_where)

No comments: