Archive

Archive for September, 2009

C# Constraints (where)

Have you ever wanted to use generic definition but with a specific type?

To add a constraint to a generic type definition, use the where.

Let’s say that you want to create a new class MyClassgeneric that use generic, but the types should be of type Item.

public class MyClass where T : Item
{
}

You can also declare a method with the where constraint as follow:

public T Get() where T : Item
{
}

Categories: C#