Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a type alias refer to another type alias? [duplicate]

Tags:

c#

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.

like image 987
screwnut Avatar asked Jun 04 '14 18:06

screwnut


2 Answers

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>;
   ...
}
like image 118
Keith Avatar answered Sep 25 '22 00:09

Keith


The best you can do is:

using Word = System.String;
using Words = System.Collections.Generic.List<System.String>;
like image 26
Derek Avatar answered Sep 23 '22 00:09

Derek