; ...">

"using" a directive like alias does not work in the same namespace

Hi i have a simple question

namespace A
{
    using TupleA = Tuple<int, int>;
    namespace B
    {       
        using DicB = Dictionary<TupleA, int>;       
    }
}    

      

This can be done .. but it is not.

namespace A
{
    using TupleA = Tuple<int, int>;
    using DicB = Dictionary<TupleA, int>;               
}    

      

Second case, the compiler cannot find the "TupleA" type! How can I get it to work? I am using .Net Framework 4.5

+3


source to share


2 answers


You cannot do the exact syntax you want to work. This is due to the following excerpt from the C # specification (see "9.4.1 Using alias directives" ):

The order in which the directive-alias-directives are written does not matter, and the resolution of the name or type name referenced by the using-alias directive is independent of the using-alias directive itself or other directive-disposing in the directly containing compiler or in space names.



In other words, the C # compiler must handle all alias directives using

in the isolated declaration space. Since the order doesn't matter, a single directive cannot refer to an alias in the same declaration space. Otherwise, the compiler will have to do multiple passes through the alias directives, iteratively declaring as many as it can and trying again until it has successfully declared all aliases or new things are announced.

As you've seen in your own test, you can declare an alias in the declaration space containing the declaration space in which you want to use it. This imposes order on the declarations and allows the use of a pseudonym.

+3


source


will work

namespace A
{
   using TupleA = System.Tuple<int, int>;
   using DicB = System.Collections.Generic.Dictionary<System.Tuple<int, int>, int>;   
}  

      

or if you should



 using TupleA = System.Tuple<int, int>;
 namespace A
 {

    using DicB = System.Collections.Generic.Dictionary<TupleA, int>;   
 }  

      

Update

MSDN related to what you are trying to do 9.3.1 Using alias directives

0


source







All Articles