123

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?

csharptest.net
  • 62,602
  • 11
  • 71
  • 89

2 Answers2

136

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
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    A using alias directive cannot have an open generic type on the right hand side. For example, you cannot create a using alias for a List, but you can create one for a List. http://msdn.microsoft.com/en-us/library/sf0df423.aspx – Sergey Mirvoda Feb 08 '11 at 18:41
  • 6
    Disappointing answer but upvoting all the same (since the answer is correct and relevant). :-/ – BrainSlugs83 Aug 30 '14 at 03:26
  • 35
    Boo. C# makes me miss C++ templates so much. And C++ template error messages make me miss C#. – Grault Jul 30 '15 at 19:28
17

as shown at http://msdn.microsoft.com/en-us/library/sf0df423.aspx and http://msdn.microsoft.com/en-us/library/c3ay4x3d%28VS.80%29.aspx, you can do

using gen = System.Collections.Generic;
using GenList = System.Collections.Generic.List<int>;

and then use

gen::List<int> x = new gen::List<int>;

or

GenList x = new GenList();

however you have to replicate those using definitions at every file where you use them, so if you make some changes to them in the future and forget to update at every file, things will break badly.

I hope C# in the future Will treat aliases like the do with extension methods and let you define many of them in a file that you use elsewhere, then maintain them at one place and hide the internal unnecessary type mapping details from the type consumers.

George Birbilis
  • 711
  • 7
  • 3
  • 13
    since somebody asked,I do know this is old discussion, but I just had the same issue and search engine took me here - why should I care if this is old discussion? Are we doing the news? – George Birbilis Jul 04 '12 at 19:16
  • 4
    I got even worse behaviour in Silverlight Forums - they just delete any posts at older discussions. They don't really get it that people come across these matters again and again and they get to discussions that have partial information cause nobody bothered to add some mental notes after they found the answer to the issue. Their excuse was it brings old discussions to top (as if there are no newer better answers or developments to be added). They should fix such forums to not bring very old topics to the front when updated unless an admin sticks them to the top instead of wasting our time... – George Birbilis Jul 04 '12 at 20:04
  • 10
    Downvoted because the answer was not helpful -- everything shown was already declared in the OP and is not a useful answer -- in fact, this knowledge is a required prerequisite to ask the OP's question in the first place. (Regarding the "old discussion" stuff, not sure how that's relevant, Stack Overflow actually rewards you for contributing to older discussions -- see the "Necromancer" badge.) – BrainSlugs83 Aug 30 '14 at 03:23
  • 2
    @BrainSlugs83 I found the first part of this answer helpful. 1) The OP does **not** mention importing a namespace with an alias as an option (so "everything shown was already declared" is an incorrect statement); 2) It's a valid option to indirectly import 'a [generic] type' without otherwise pollution of 'all namespaces'; the "downside" is needing to add a prefix, which is a simple trade-off. Using an alias also [avoids pulling in extension methods that may also exist](https://stackoverflow.com/questions/3337453/why-do-extension-methods-not-work-with-namespace-aliasing). – user2864740 Jul 03 '18 at 18:24