Using Alias ​​= Class; with generics

In C #, you can use alias classes using the following:

using Str = System.String; 

This is possible with classes that are generic dependent, I tried a similar approach, but it doesn't seem to compile.

 using IE<T> = System.Collections.Generic.IEnumerable<T>; 

and

 using IE = System.Collections.Generic.IEnumerable; 

Is this possible using generics in C #? If so, what am I missing?

+6
source share
2 answers

This is possible with classes that are generic dependent, I tried a similar approach, but it doesn't seem to compile.

 using IE<T> = System.Collections.Generic.IEnumerable<T>; 

No, It is Immpossible. The only thing that works is this:

  using IE = System.Collections.Generic.IEnumerable<string>; 

A using declaration cannot be shared.

+10
source

No, It is Immpossible. C # Language Specification , Version 5, Section 9.4.1:

Using aliases may call a private constructed type, but cannot name an unrelated declaration of a general type without providing type arguments. For example:

 namespace N1 { class A<T> { class B {} } } namespace N2 { using W = N1.A; // Error, cannot name unbound generic type using X = N1.AB; // 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 } 
+5
source

All Articles