Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Alias = Class; with generics [duplicate]

Tags:

c#

alias

generics

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?

like image 505
bizzehdee Avatar asked Feb 17 '14 14:02

bizzehdee


2 Answers

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.

like image 150
Konrad Rudolph Avatar answered Oct 30 '22 06:10

Konrad Rudolph


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
}
like image 34
Damien_The_Unbeliever Avatar answered Oct 30 '22 04:10

Damien_The_Unbeliever