在“使用”中使用别名
问题描述:
我想为复杂类型定义自己的别名。我很好奇为什么编译器不能识别已经导入的类型。例如:在“使用”中使用别名
作品:
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>>;
答
试试这个:
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
using myCollection = List<Dictionary<string, string>>;
}
using
指令不能引用在同一范围内进口的类型。上面的例子是有效的,因为最后的using
指令只引用了外部作用域中导入的类型。
+1
+1我知道使用可以做别名,但从来没有想到它会像这样具体化类型,而且范围也是新闻。好信息! – 2010-09-13 20:00:50
C#中没有typedef! – 2010-09-13 19:28:13
@ClausJørgensen:与'using'混淆非常接近。你只需要在每个文件中重复它,因为没有'#include'。 – dtb 2010-09-13 19:30:48
是的,但他明确要求的是typedef,而不是命名空间的缩写。差别很大,即使是C++开发人员也倾向于滥用typedef来实现它的目的。 – 2010-09-13 20:44:06