Q:

C#: How to design a generic class so that the type parameter must inherit a certain class?

I've written a class that looks like this:
public class MyClass<T>
{
    public void doSomething()
    {
       T.somethingSpecial;
    }
}

This code doesn't compile because the compiler has no idea what T is.  I would like to constrain T so that it must inherit a certain class that defines somethingSpecial.  Bonus points if you can tell me how to do the same thing by contraining T so that it must implement a certain interface.

A:

public class MyClass<T> where T: IAmSomethingSpecial

It's called Constraints on Type Parameters.

A:

Use the following type parameter constraint in the class declaration:
public class MyClass<T> where T : MyBaseClass

You can read more about type parameter contraints for example here at MSDN.

A:

What you want is a generic constraint:
public class MyClass<T> where T : SomeParentClass

