C#名称空间别名-重点是什么?
我一直在尝试了解有关C#语言的更多信息,但是我还没有看到有人使用命名空间别名的情况,例如
using someOtherName = System.Timers.Timer;
在我看来,这只会增加对语言的理解的混乱。
有人可以解释一下吗?
回答:
那是类型别名,而不是名称空间别名。消除歧义很有用-例如,针对:
using WinformTimer = System.Windows.Forms.Timer;using ThreadingTimer = System.Threading.Timer;
(ps:感谢您选择Timer
;-p)
否则,如果您同时使用System.Windows.Forms.Timer
,并System.Timers.Timer
在同一文件中,你不得不继续给的全名(因为Timer
可能会造成混乱)。
extern
对于使用不同程序集中具有相同完全限定类型名称的类型,它也起了别名的作用-很少见,但受支持很有用。
实际上,我可以看到另一种用法:当您想快速访问类型但又不想使用常规时,using
因为您不能导入一些冲突的扩展方法…有点费解,但是…这是一个示例…
namespace RealCode { //using Foo; // can't use this - it breaks DoSomething
using Handy = Foo.Handy;
using Bar;
static class Program {
static void Main() {
Handy h = new Handy(); // prove available
string test = "abc";
test.DoSomething(); // prove available
}
}
}
namespace Foo {
static class TypeOne {
public static void DoSomething(this string value) { }
}
class Handy {}
}
namespace Bar {
static class TypeTwo {
public static void DoSomething(this string value) { }
}
}
以上是 C#名称空间别名-重点是什么? 的全部内容, 来源链接: utcz.com/qa/408853.html