C# using alias as type parameter in other using alias
Asked Answered
V

6

9

I'm trying to define a pair of type aliases at the top of my C# program. This is a short example of what I'm trying to do:

using System;
using System.Collections.Generic;

namespace Foo {
    using TsvEntry = Dictionary<string, string>;
    using Tsv = List<TsvEntry>;
}

When I try to compile this using mcs 3.2.8.0, I get the following error message:

foo.cs(6,19): error CS0246: The type or namespace name `TsvEntry' could not be found. Are you missing an assembly reference?

Is it possible to use using aliases within other aliases in C#, or am I missing something about the way using statements work?

Variation answered 10/3, 2016 at 16:17 Comment(0)
C
8

Check documentation for this question:

https://msdn.microsoft.com/en-us/library/aa664765(v=vs.71).aspx

It says:

The order in which using-alias-directives are written has no significance, and resolution of the namespace-or-type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other using-directives in the immediately containing compilation unit or namespace body. In other words, the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing compilation unit or namespace body had no using-directives. In the example

namespace N1.N2 {}
namespace N3
{
   using R1 = N1;         // OK
   using R2 = N1.N2;      // OK
   using R3 = R1.N2;      // Error, R1 unknown
}

the last using-alias-directive results in a compile-time error because it is not affected by the first using-alias-directive.

Technically, you cannot do it same namespace, but if you do alias in namespace 1, and do alias for this alias in a nested namespace, it will work:

namespace N1
{
    namespace N12 { }
}

namespace N2
{
    using R1 = N1;

    namespace N2
    {
        using R2 = R1.N12;
    }
}

I am not really sure it's worth using aliases in your specific example, consider using them as rare as you can, mostly for resolving namespace conflicts.

Cayman answered 10/3, 2016 at 16:24 Comment(0)
L
4

Just as an addition to the other answers.

If you really want to continue your way, nesting the namespaces would be the way to go. Since for the second namespace, the first using would then be defined.

The following would work since you are in a deeper namespace:

namespace N1
{
    using TsvEntry = Dictionary<string, string>;

    namespace N1.N2
    {
        using Tsv = List<TsvEntry>;

    }
}

Please note that although it is working, i wouldn't recommend using this kind of code structure since you can end up with a deeply nested code.

Lenzi answered 10/3, 2016 at 16:29 Comment(0)
S
3

From MSDN's Using alias directives :

The order in which using-alias-directives are written has no significance, and resolution of the namespace-or-type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other using-directives in the immediately containing compilation unit or namespace body. In other words, the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing compilation unit or namespace body had no using-directives.

// emphasis mine

In your example, the last using-alias-directive results in a compile-time error because it is not affected by the first using-alias-directive.

Sienese answered 10/3, 2016 at 16:24 Comment(0)
E
2

As others have mentioned, using Alias this way is not possible, however, you can get a similar functionality using class inheritance.

Using your example, you could do something like this:

using System;
using System.Collections.Generic;

namespace Foo {
    class TsvEntry : Dictionary<string, string> {}
    class Tsv : List<TsvEntry> {}
}

This has the additional advantage that you can easily add extra functionality to these classes later on as you need it.

Engraving answered 30/8, 2017 at 14:0 Comment(0)
P
1

(Adding this here because I ran across this question a lot when searching for a related issue, so it might help others)

If you're looking to use aliases to functions for a functional style of programming, what you're looking for is the delegate keyword. You can think of delegate as making an interface, except it's for a function.

so instead of using Expression = Func<string>; using Converter = Func<List<string>, Expression>; you do delegate string Expression(); delegate Expression Converter(List<string> tokenizedExpressions);

https://weblogs.asp.net/dixin/functional-csharp-function-type-and-delegate

Postiche answered 26/4, 2019 at 6:8 Comment(0)
S
0

Rather than using Foo as namespace you could follow recommendations that suggest to include a company name and for example a product name. You end up with a namespace like Fabrikam.Math. The following doesn't look anymore as nesting just to achieve aliasing:

using System;
using System.Collections.Generic;
namespace Fabrikam
{
    using TsvEntry = Dictionary<string, string>;
    namespace Math
    {
        using Tsv = List<TsvEntry>;
        internal class Program
        {
            static void Main()
            {
                var list = new Tsv();
                Console.WriteLine($"Number of elements: {list.Count}");
                Console.ReadLine();
             }
         }
    }
}
Shockproof answered 18/12, 2021 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.