在哪里或何时需要使用命名空间别名?
using someOtherName = System.Timers.Timer;
我认为这只会增加理解该语言的混乱。
在哪里或何时需要使用命名空间别名?
using someOtherName = System.Timers.Timer;
我认为这只会增加理解该语言的混乱。
using WinformTimer = System.Windows.Forms.Timer;
using ThreadingTimer = System.Threading.Timer;
System.Windows.Forms.Timer
和System.Threading.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) { }
}
}
System.Timers.Timer
;-p - Marc Gravell当我有多个命名空间具有冲突的子命名空间和/或对象名称时,我会使用它,你可以像这样做 [举例]:
using src = Namespace1.Subspace.DataAccessObjects;
using dst = Namespace2.Subspace.DataAccessObjects;
...
src.DataObject source = new src.DataObject();
dst.DataObject destination = new dst.DataObject();
否则必须编写以下内容:
Namespace1.Subspace.DataAccessObjects.DataObject source =
new Namespace1.Subspace.DataAccessObjects.DataObject();
Namespace2.Subspace.DataAccessObjects.DataObject dstination =
new Namespace2.Subspace.DataAccessObjects.DataObject();
这可以减少大量的输入,并且可以用来使代码更易读。
除了提到的例子之外,当需要反复引用通用类型时,类型别名(而不是命名空间别名)也很方便:
Dictionary<string, SomeClassWithALongName> foo = new Dictionary<string, SomeClassWithALongName>();
private void DoStuff(Dictionary<string, SomeClassWithALongName> dict) {}
对比:
using FooDict = Dictionary<string, SomeClassWithALongName>;
FooDict foo = new FooDict();
private void DoStuff(FooDict dict) {}
简洁性。
为共享类型名称的命名空间提供清晰度是其附带好处,但本质上它只是糖果。
我经常在这种情况下使用它
using Utility = MyBaseNamespace.MySubNamsepace.Utility;
在其他情况下(例如MyBaseNamespace.MySubNamespace.MySubSubNamespace.Utility
),Utility
将具有不同的上下文,但我期望/更喜欢Utility
始终指向那个特定的类。
当您在多个包含命名空间中有多个具有相同名称的类时,它非常有用。例如...
namespace Something.From.SomeCompanyA {
public class Foo {
/* ... */
}
}
namespace CompanyB.Makes.ThisOne {
public class Foo {
/* ... */
}
}
您可以使用别名来让编译器更加顺畅,并使您和团队中的其他人更加清晰明了:
using CompanyA = Something.From.CompanyA;
using CompanyB = CompanyB.Makes.ThisOne;
/* ... */
CompanyA.Foo f = new CompanyA.Foo();
CompanyB.Foo x = new CompanyB.Foo();
using System.Web.WebControls;
// lots of other using statements
// contains the domain model for project X
using dom = Company.ProjectX.DomainModel;
// contains common web functionality
using web = Company.Web;
// etc.
// User from the domain model
dom.User user = new dom.User();
// Data transfer object
dto.User user = new dto.User();
// a global helper class
utl.SomeHelper.StaticMethod();
// a hyperlink with custom functionality
// (as opposed to System.Web.Controls.HyperLink)
web.HyperLink link = new web.HyperLink();
using System.Windows.Forms;
和using System.Windows.Input;
,当你想要访问ModifierKeys
时,你可能会发现ModifierKeys
这个名称在System.Windows.Forms.Control
和System.Windows.Input
这两个命名空间中都存在。因此,通过声明using Input = System.Windows.Input;
,你可以通过Input.ModifierKeys
来获取System.Windows.Input.ModifierKeys
。using System.Data;
using SqlDataCon = System.Data.SqlClient.SqlConnection
MyClass myClassUT;
假设你想为一个包含静态方法的静态类编写单元测试,该类名为myClassUT
。那么你可以创建一个别名,如下所示:
using MyStaticClassUT = Namespace.MyStaticClass;
然后,您可以像这样编写单元测试:
public void Test()
{
var actual = MyStaticClassUT.Method();
var expected = ...
}
并且您永远不会失去对测试主题的关注。
using int = System.Int32
怎么样?很有用吧?这与其他地方可以利用相同的用途。 - nawfalusing int = System.Int32
这样的东西,并在声明文件之外的其他地方使用它。因此,这个int
到Int32
的别名可能通过其他方式实现,或者是编译器/运行时中的特殊事物。 - KFLint
别名并不是按照你所描述的方式实现的。它是误导性的,因为你暗示类型别名可以像int
覆盖Int32
一样在全局范围内使用。 - KFL