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");
}
}
}
精彩评论