views:

56

answers:

3

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?

+3  A: 
using MyIndex = Dictionary<MyKey, MyClass>;
Steven
+5  A: 

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();
        }
    }
}
Rob
Thankyou Rob and others. Next time I am in an interview I will know that it is called a using alias. Answer now baked into my code.
camelCase
Forgot to say I think the alias (using) should be declared within an application namespace region.
camelCase
+2  A: 

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

    }
}
Bablo