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 指令到底是做什么的?
基础教程推荐
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 获取C#保存对话框的文件路径 2022-01-01
- 将数据集转换为列表 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 如果条件可以为空 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
