C# Nested Type Aliases – Cannot Resolve Symbol

Issue

Why doesn’t this compile in Unity 2018 C#?

namespace MyNS
{
    using FooTable = Dictionary<int, float[]>;
    using BarTable = Dictionary<int, FooTable>;
} 

Likewise, if I omit the namespace:

using FooTable = System.Collections.Generic.Dictionary<int, float[]>;
using BarTable = System.Collections.Generic.Dictionary<int, FooTable>;

I get the same compiler error.

(Frankly I don’t know why the Dictionary qualifiers are optional inside the namespace, yet mandatory outside!)

The compiler error is for the last symbol in the using BarTable... line: "Cannot resolve symbol FooTable".

Is it not possible to nest type aliases in this way?

Solution

It is just not allowed by the compiler:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#remarks

Create a using alias directive to make it easier to qualify an
identifier to a namespace or type. In any using directive, the
fully-qualified namespace or type must be used regardless of the using
directives that come before it. No using alias can be used in the
declaration of a using directive. For example, the following generates
a compiler error:

using s = System.Text;
using s.RegularExpressions; // Generates a compiler error.

Answered By – fafase

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.