Using a 'using alias = class' with generic types?

csharptest.net picture csharptest.net · Feb 8, 2011 · Viewed 28.9k times · Source

So sometimes I want to include only one class from a namespace rather than a whole namespace, like the example here I create a alias to that class with the using statement:

using System;
using System.Text;
using Array = System.Collections.ArrayList;

I often do this with generics so that I don't have to repeat the arguments:

using LookupDictionary = System.Collections.Generic.Dictionary<string, int>;

Now I want to accomplish the same with a generic type, while preserving it as a generic type:

using List<T> = System.Collections.Generic.List<T>;

But that doesn't compile, so is there any way to achieve creating this alias while leaving the type as generic?

Answer

JaredPar picture JaredPar · Feb 8, 2011

No there is not. A type alias in C# must be a closed (aka fully resolved) type so open generics are not supported

This is covered in section 9.4.1 of the C# Language spec.

Using aliases can name a closed constructed type, but cannot name an unbound generic type declaration without supplying type arguments.

namespace N2
{
    using W = N1.A;         // Error, cannot name unbound generic type
    using X = N1.A.B;       // Error, cannot name unbound generic type
    using Y = N1.A<int>;    // Ok, can name closed constructed type
    using Z<T> = N1.A<T>;   // Error, using alias cannot have type parameters
}