Search
Close this search box.

C#/.NET Little Wonders: Constraining Generics with Where Clause

Back when I was primarily a C++ developer, I loved C++ templates.  The power of writing very reusable generic classes brought the art of programming to a brand new level. 

Unfortunately, when .NET 1.0 came about, they didn’t have a template equivalent.  With .NET 2.0 however, we finally got generics, which once again let us spread our wings and program more generically in the world of .NET

However, C# generics behave in some ways very differently from their C++ template cousins.  There is a handy clause, however, that helps you navigate these waters to make your generics more powerful.

The Problem – C# Assumes Lowest Common Denominator

In C++, you can create a template and do nearly anything syntactically possible on the template parameter, and C++ will not check if the method/fields/operations invoked are valid until you declare a realization of the type.  Let me illustrate with a C++ example:

// compiles fine, C++ makes no assumptions as to T
template <typename T>
class ReverseComparer {
  public : int Compare(const T& lhs, const T& rhs) {
    return rhs.CompareTo(lhs);
  }
};

Notice that we are invoking a method CompareTo() off of template type T.  Because we don’t know at this point what type T is, C++ makes no assumptions and there are no errors.

C++ tends to take the path of not checking the template type usage until the method is actually invoked with a specific type, which differs from the behavior of C#:

// this will NOT compile!  C# assumes lowest common denominator.
public class ReverseComparer<T> {
  public int Compare(T lhs, T rhs) {
    return lhs.CompareTo(rhs);
  }
}

So why does C# give us a compiler error even when we don’t yet know what type is?  This is because C# took a different path in how they made generics.  Unless you specify otherwise, for the purposes of the code inside the generic method, T is basically treated like an object (notice I didn’t say isan object).

That means that any operations, fields, methods, properties, etc that you attempt to use of type T must be available at the lowest common denominator type: object

Now, while object has the broadest applicability, it also has the fewest specific.  So how do we allow our generic type placeholder to do things more than just what object can do?

Solution: Constraint the Type With Where Clause

So how do we get around this in C#?  The answer is to constrain the generic type placeholder with the where clause.  Basically, the where clause allows you to specify additional constraints on what the actual type used to fill the generic type placeholder must support.

You might think that narrowing the scope of a generic means a weaker generic.  In reality, though it limits the number of types that can be used with the generic, it also gives the generic more power to deal with those types.  In effect these constraints says that if the type meets the given constraint, you can perform the activities that pertain to that constraint with the generic placeholders.

Constraining Generic Type to Interface or Superclass

One of the handiest where clause constraints is the ability to specify the type generic type must implement a certain interface or be inherited from a certain base class.

For example, you can’t call CompareTo() in our first C# generic without constraints, but if we constrain T to IComparable<T>, we can:

public class ReverseComparer<T>
    where T : IComparable<T> {
  public int Compare(T lhs, T rhs) {
    return lhs.CompareTo(rhs);
  }
}

Now that we’ve constrained T to an implementation of IComparable<T>, this means that our variables of generic type T may now call any members specified in IComparable<T> as well.  This means that the call to CompareTo() is now legal.

If you constrain your type, also, you will get compiler errors immediately if you attempt to use a type that doesn’t meet the constraint.  This tends to be much clearer than the syntax error you would get within C++ template code itself when you used a type not supported by a C++ template.

Constraining Generic Type to Only Reference Types

Sometimes, you want to assign an instance of a generic type to null, but you can’t do this without constraints, because you have no guarantee that the type used to realize the generic is not a value type, where null is meaningless.

Well, we can fix this by specifying the class constraint in the where clause.  By declaring that a generic type must be a class, we are saying that it is a reference type, and this allows us to assign null to instances of that type:

public static class ObjectExtensions {
  public static TOut Maybe<TIn, TOut>(this TIn value, Func<TIn, TOut> accessor)
      where TOut : class
      where TIn : class {
    return (value != null) ? accessor(value) : null;
  }
}

In the example above, we want to be able to access a property off of a reference, and if that reference is null, pass the null on down the line.  To do this, both the input type and the output type must be reference types (yes, nullable value types could also be considered applicable at a logical level, but there’s not a direct constraint for those).

Constraining Generic Type to only Value Types

Similarly to constraining a generic type to be a reference type, you can also constrain a generic type to be a value type.  To do this you use the struct constraint which specifies that the generic type must be a value type (primitive, structenum, etc).

Consider the following method, that will convert anything that is IConvertible (int, double, string, etc) to the value type you specify, or null if the instance is null.

public static T? ConvertToNullable<T>(IConvertible value)
    where T : struct {
  T? result = null;

  if (value != null) {
    result = (T)Convert.ChangeType(value, typeof(T));
  }

  return result;
}

Because T was constrained to be a value type, we can use T? (System.Nullable<T>)where we could not do this if T was a reference type.

Constraining Generic Type to Require Default Constructor

You can also constrain a type to require existence of a default constructor.  Because by default C# doesn’t know what constructors a generic type placeholder does or does not have available, it can’t typically allow you to call one.  That said, if you give it the new() constraint, it will mean that the type used to realize the generic type must have a default (no argument) constructor.

Let’s assume you have a generic adapter class that, given some mappings, will adapt an item from type TFrom to type TTo.  Because it must create a new instance of type TTo in the process, we need to specify that TTo has a default constructor:

// Given a set of Action<TFrom,TTo> mappings will map TFrom to TTo
public class Adapter<TFrom, TTo> : IEnumerable<Action<TFrom, TTo>>
    where TTo : class, new() {
  // The list of translations from TFrom to TTo
  public List<Action<TFrom, TTo>> Translations { get; private set; }

  // Construct with empty translation and reverse translation sets.
  public Adapter() {
    // did this instead of auto-properties to allow simple use of initializers
    Translations = new List<Action<TFrom, TTo>>();
  }

  // Add a translator to the collection, useful for initializer list
  public void Add(Action<TFrom, TTo> translation) {
    Translations.Add(translation);
  }

  // Add a translator that first checks a predicate to determine if the
  // translation should be performed, then translates if the predicate returns
  // true
  public void Add(Predicate<TFrom> conditional,
                  Action<TFrom, TTo> translation) {
    Translations.Add((from, to) => {
      if (conditional(from)) {
        translation(from, to);
      }
    });
  }

  // Translates an object forward from TFrom object to TTo object.
  public TTo Adapt(TFrom sourceObject) {
    var resultObject = new TTo();

    // Process each translation
    Translations.ForEach(t => t(sourceObject, resultObject));

    return resultObject;
  }

  // Returns an enumerator that iterates through the collection.
  public IEnumerator<Action<TFrom, TTo>> GetEnumerator() {
    return Translations.GetEnumerator();
  }

  // Returns an enumerator that iterates through a collection.
  IEnumerator IEnumerable.GetEnumerator() {
    return GetEnumerator();
  }
}

Notice, however, you can’t specify any other constructor, you can only specify that the type has a default (no argument) constructor.

Summary

The where clause is an excellent tool that gives your .NET generics even more power to perform tasks higher than just the base “object level” behavior. 

There are a few things you cannot specify with constraints (currently) though:

  • Cannot specify the generic type must be an enum.
  • Cannot specify the generic type must have a certain property or method without specifying a base class or interface – that is, you can’t say that the generic must have a Start() method.
  • Cannot specify that the generic type allows arithmetic operations.
  • Cannot specify that the generic type requires a specific non-default constructor.

In addition, you cannot overload a template definition with different, opposing constraints.  For example you can’t define a Adapter<T> where T : struct and Adapter<T> where T : class

Hopefully, in the future we will get some of these things to make the where clause even more useful, but until then what we have is extremely valuable in making our generics more user friendly and more powerful!

Technorati Tags: C#,.NET,Little Wonders,BlackRabbitCoder,where,generics

This article is part of the GWB Archives. Original Author:  James Michael Hare

Related Posts