What does the using directive do, exactly?(using 指令到底是做什么的?)
问题描述
在 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.如果没有使用指令来创建 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 指令到底是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:using 指令到底是做什么的?
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
