A generic class allows you to write your class without committing to any type, yet allows the user of your class, later on, to indicate the specific type to be used. While this gives greater flexibility by placing some constraints on the types that may be used for the parameterized type, you gain some control in writing your class. Let's look at an example:

Example 1. The need for constraints: code that will not compile


public static T Max<T>(T op1, T op2)
{
        if (op1.CompareTo(op2) < 0)
                return op1;
        return op2;
}


The code in Example 1 will produce a compilation error:

Error 1 'T' does not contain a definition for 'CompareTo'

Assume I need the type to support the CompareTo() method. I can specify this by using the constraint that the type specified for the parameterized type must implement the IComparable interface. Example 2 has the code:

Example 2. Specifying a constraint


public static T Max<T>(T op1, T op2) where T : IComparable
{
        if (op1.CompareTo(op2) < 0)
                return op1;
        return op2;
}

In Example 2, I have specified the constraint that the type used for parameterized type must inherit from (implement) IComparable. The following are some of the other constraints may be used:


where T : struct                   type must be a value type (a struct)
where T : class                   type must be reference type (a class)
where T : new()                  type must have a no-parameter constructor
where T : class_name      type may be either class_name or one of its
                                              sub-classes (or is below class_name
                                              in the inheritance hierarchy)

where T : interface_name  type must implement the specified interface
You may specify a combination of constraints, as in: where T : IComparable, new(). This says that the type for the parameterized type must implement the IComparable interface and must have a no-parameter constructor.

------

posted on Thursday, March 13, 2008 12:25 PM
Filed Under [ BizTalk, WCF, Design Patterns, C#, Software Factories C# general ]
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

No comments posted yet.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: