开发者

Shorthand declaration of long generic collection types

开发者 https://www.devze.com 2023-01-14 06:59 出处:网络
I have looked at a lot of example c# generic code and remember seeing a syntactic declaration trick that created an alternative shorthand type for a long ge开发者_如何学Goneric dictionary type. Mixing

I have looked at a lot of example c# generic code and remember seeing a syntactic declaration trick that created an alternative shorthand type for a long ge开发者_如何学Goneric dictionary type. Mixing C# and C++ it was something like:

typedef MyIndex as Dictionary< MyKey, MyClass>;

This then allowed the following usage:

class Foo
{
    MyIndex _classCache = new MyIndex();
}

Can someone remind me which C# lanaguage feature supports this?


It's this, another form of the using directive, used to define an alias.

using MyClass = System.Collections.Generic.Dictionary<string, int>;

namespace MyClassExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var instanceOfDictionaryStringInt = new MyClass();
        }
    }
}


Here is an example of how its done

using Test = System.Collections.Generic.Dictionary<int, string>;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Test myDictionary = new Test();
            myDictionary.Add(1, "One");
        }

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消