6

When doing this in C#:

using Word = System.String;
using Words = System.Collections.Generic.List<Word>;

The compiler complains that "the type or namespace 'Word' could not be found". Is this not possible? If it makes a difference, I'm trying to do this in the global namespace.

EDIT: just found another SO that answers this: Why can one aliased C# Type not be accessed by another? This can marked as duplicate.

Community
  • 1
  • 1
screwnut
  • 1,367
  • 7
  • 26

2 Answers2

5

No. These aliases only apply within the namespace body.

Source

You can work around this limitation by moving the 2nd using inside the namespace body like this:

using Word = System.String;

namespace MyNamespace {
   using Words = System.Collections.Generic.List<Word>;
   ...
}
Keith
  • 20,636
  • 11
  • 84
  • 125
  • So, is there a solution to this? Moving these aliases within a named namespace makes no difference. – screwnut Jun 04 '14 at 18:25
  • That's an interesting nugget of info that, FWICT, is not in the duplicated SO answer. Green check for you! – screwnut Jun 04 '14 at 22:16
1

The best you can do is:

using Word = System.String;
using Words = System.Collections.Generic.List<System.String>;
Derek
  • 7,615
  • 5
  • 33
  • 58