在C#中自动导入嵌套的命名空间 [英] Importing nested namespaces automatically in C#
问题描述
很抱歉,如果这个问题被问了。 我开始学习C#,发现C#不自动导入嵌套的命名空间。 我不明白:
Sorry if this question was asked already. I started studying C# and noticed that C# doesn't automatically import nested namespaces. I don't understand:
using System;
应该自动导入包含在System命名权的所有类? 所以应该不用我来写
should automatically import all classes contained in the System namespace right? So there should be no need for me to write
using System.Windows.Form;
我会用Windows.Form明白,如果
甚至工作。但是,编译器无法解析吧!什么是的点使用系统;它
之前呢?
那么,为什么使用系统;
不导入 System.Windows
自动以及 System.Windows .Forms
- 对不起,如果这个词导入错在这里..也许转移到全局命名空间是正确的术语
I would understand if using Windows.Form
even worked. But the compiler could not resolve it! What is the point of the using System;
before it then?
So why does using System;
not import System.Windows
automatically as well as System.Windows.Forms
- sorry if the word import is wrong here.. maybe move to global namespace is the right terminology.
推荐答案
C#是不是Java。
C# is not Java.
一个 使用
指令是用,所以你不必在一个类型的完全限定名称输入。这也有助于消除歧义类型名称(使用别名为实例)。
A using
directive is used so you don't have to type in the fully qualified name of a type. It also helps with disambiguating type names (using aliases for instance).
在控制台的情况下
,例如,你并不需要键入 System.Console
。
In the case of Console
, for example, you don't need to type System.Console
.
要理解一个命名空间和组件之间的区别是很重要的 - 一个命名空间是一个的逻辑的类型分组。一个组件是物理的类型分组。命名空间可以跨越组件。
It is important to understand the difference between a namespace and an assembly - a namespace is a logical grouping of types. An assembly is a physical grouping of types. Namespaces can span assemblies.
在引用的组件(这更像是在Java中导入一个包),你获得的所有的市民类型吧。为了使用一个类型需要唯一地识别它。这是通过命名空间做 - 的使用
指令只是意味着你不必键入类型的完全限定名
When you reference an assembly (this is more like importing a package in Java), you gain access to all of the public types in it. In order to use a type you need to uniquely identify it. This is done through the namespace - the using
directive simply means you don't have to type the fully qualified name of the type.
这篇关于在C#中自动导入嵌套的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!