Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand declaration of long generic collection types

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 generic 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?

like image 855
camelCase Avatar asked Sep 05 '10 12:09

camelCase


2 Answers

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();
        }
    }
}
like image 112
Rob Avatar answered Nov 02 '22 13:11

Rob


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");
        }

    }
}
like image 33
Bablo Avatar answered Nov 02 '22 13:11

Bablo