using指令的作用是什么? [英] What does the using directive do, exactly?
问题描述
在MSDN上,我可以阅读它的功能,但是我想知道它在技术上的作用(告诉编译器在哪里寻找类型..)?我的意思是用作指令.
On MSDN I can read what it does, but I would like to know what it does technically (tells compiler where to look for types..)? I mean using as a directive.
推荐答案
using
指令的主要功能是使命名空间中的类型可用,而无需限制用户代码.它考虑在引用程序集和正在编译的项目中定义的名称空间和类型的集合.
The primary function of the using
directive is to make types within a namespace available without qualification to the user code. It considers the set of namespaces and types which are defined in referenced assemblies and the project being compiled.
以MyTypes.Dll中的以下定义为例
Take for example the following definition in MyTypes.Dll
namespace MyTypes {
class Class1 {}
}
现在考虑从具有不同名称空间的另一个项目中引用MyTypes.dll
.如果没有using指令来创建Class1
,则我需要限定名称
Now consider referencing MyTypes.dll
from another project with a different namespace. Without a using directive to create Class1
i need to qualify the name
MyTypes.Class1 local1 = new MyTypes.Class1();
using
指令可让我删除此资格
The using
directive lets me remove this qualification
using MyTypes;
...
Class1 local1 = new Class1();
这篇关于using指令的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!