6

In C# it is possible to alias classes using the following:

using Str = System.String;

Is this possible with classes that depend on generics, i have tried a similar approach but it does not seem to compile.

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

and

using IE = System.Collections.Generic.IEnumerable;

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

bizzehdee
  • 20,289
  • 11
  • 46
  • 76
  • 1
    Maybe http://stackoverflow.com/questions/3720222/using-statement-with-generics-using-iset-system-collections-generic-iset can help? – NWard Feb 17 '14 at 14:37

2 Answers2

10

Is this possible with classes that depend on generics, i have tried a similar approach but it does not seem to compile.

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

No, this isn’t possible. The only thing that works is this:

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

A using declaration cannot be generic.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
5

No, it's not possible. C# Language Specification, version 5, section 9.4.1 states:

Using aliases can name a closed constructed type, but cannot name an unbound generic type declaration without supplying 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.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
}
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448