2010-09-05 75 views
3

我已經看過很多示例c#泛型代碼,並且記得看到一個爲長泛型字典類型創建替代速記類型的語法聲明技巧。混合使用C#和C++是一樣的東西:長泛型集合類型的速記聲明

typedef MyIndex as Dictionary< MyKey, MyClass>; 

這然後允許以下用法:

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

可有人提醒我這C#lanaguage功能支持呢?

+0

哇一個例子,這是我只是不會做。出於某種原因,C#沒有宏。 – 2010-09-05 13:32:28

回答

9

就是這樣,using directive的另一種形式,用於定義一個別名。

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

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

Thankyou Rob等人。下次我接受採訪時,我會知道它被稱爲使用別名。現在回答我的代碼。 – camelCase 2010-09-05 17:05:08

+0

忘了說我認爲別名(使用)應該在應用程序命名空間區域內聲明。 – camelCase 2010-09-05 17:09:47

4
using MyIndex = Dictionary<MyKey, MyClass>; 
2

下面是它如何做

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

    } 
}