Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a 'using alias = class' with generic types? [duplicate]

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?

like image 308
csharptest.net Avatar asked Feb 08 '11 18:02

csharptest.net


1 Answers

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 } 
like image 86
JaredPar Avatar answered Oct 06 '22 01:10

JaredPar