什么是C#"使用"指令?

Ron*_*rby 13 c# using using-directives

我在代码示例中看到了这个C#using语句:

using StringFormat=System.Drawing.StringFormat;
Run Code Online (Sandbox Code Playgroud)

那是什么意思?

Sea*_*ean 32

这是将typename别名为较短的名称.相同的语法也可用于别名命名空间.请参阅using指令.

(更新以回应理查德)


mbi*_*ard 13

它是别名,从现在开始,用户可以使用StringFormat来引用System.Drawing.StringFormat.如果您不想使用整个命名空间(例如,在名称冲突问题的情况下),这将非常有用.

来源:使用MSDN的指令文章


Bul*_*nes 7

也许在另一个名称空间(如Acme.Stuff)中声明了一个不同的,无关的StringFormat.如果是这种情况,这会引起混淆:

using System.Drawing; // Contains StringFormat type.
using Acme.Stuff;  // Contains another StringFormat type.

private void Foo()
{
    StringFormat myFormat = new StringFormat(); // which one to use?
}
Run Code Online (Sandbox Code Playgroud)

别名是在StringFormat = System.Drawing.StringFormat上使用,清除了一些混乱.