2010-09-13 41 views
1

我想爲複雜類型定義自己的別名。我很好奇爲什麼編譯器不能識別已經導入的類型。例如:在「使用」中使用別名

作品:

using System; 
using System.Collections.Generic; 

using myCollection = System.Collections.Generic.List 
        <System.Collections.Generic.Dictionary<string, string>>; 

錯誤:

using System; 
using System.Collections.Generic; 

using myCollection = List<Dictionary<string, string>>; 
+0

C#中沒有typedef! – 2010-09-13 19:28:13

+1

@ClausJørgensen:與'using'混淆非常接近。你只需要在每個文件中重複它,因爲沒有'#include'。 – dtb 2010-09-13 19:30:48

+0

是的,但他明確要求的是typedef,而不是命名空間的縮寫。差別很大,即使是C++開發人員也傾向於濫用typedef來實現它的目的。 – 2010-09-13 20:44:06

回答

9

試試這個:

using System; 
using System.Collections.Generic; 

namespace ConsoleApplication1 
{ 
    using myCollection = List<Dictionary<string, string>>; 
} 

using指令不能引用在同一範圍內進口的類型。上面的例子是有效的,因爲最後的using指令只引用了外部作用域中導入的類型。

+1

+1我知道使用可以做別名,但從來沒有想到它會像這樣具體化類型,而且範圍也是新聞。好信息! – 2010-09-13 20:00:50