C# using alias as type parameter in other using alias

前端 未结 5 1204
既然无缘
既然无缘 2021-01-13 01:33

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.Colle         


        
相关标签:
5条回答
  • 2021-01-13 02:00

    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.

    0 讨论(0)
  • 2021-01-13 02:04

    (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

    0 讨论(0)
  • 2021-01-13 02:13

    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.

    0 讨论(0)
  • 2021-01-13 02:20

    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.

    0 讨论(0)
  • 2021-01-13 02:22

    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.

    0 讨论(0)
提交回复
热议问题