关于 C# 命名空间结构的困惑
我对 C# 中命名空间的结构有点困惑 我是 C# 和 WPF 的新手,
当我创建 WPF 的新项目时,默认情况下这些命名空间包含在顶部,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
我对它们的结构感到困惑。 我明白这
using System;
意味着我指的是系统命名空间
但是其他人呢,比如
using System.Text;
这意味着什么?
- 我正在访问名为“System.Text”的单个命名空间?
- 或者我正在访问嵌套在“System”命名空间内的“Text”命名空间?
另外,如果“文本”嵌套在“系统”内,为什么只有“使用系统”包含所有命名空间?我们是否应该显式地包含我们使用的每个名称空间?
I am little confused about the structure of Namespace in C#
I'm new to C# and WPF
When i create a new project of WPF, by default these Namespaces are included at the top
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
I'm confused understanding the structures of these.
I understand that
using System;
Means that im referring to System namespace
But what about the others like
using System.Text;
What that means?
- I'm accessing a single namespace named as "System.Text" ?
- or I'm accessing "Text" namespace nested inside "System" namespace ?
Also if "Text" is nested inside "System" why only "using System" includes all the namespaces ? should we have to explicitly include every namespace we use ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
后者。您正在使用嵌套在“System”命名空间内的“Text”命名空间。
请注意,当您说
“这只是一种简短的书写方式”
时,“命名空间”只有“简单”名称。
通常你会这样做,是的。在某些情况下您可能不想这样做。例如,如果您有两个名称空间,它们都包含同名的类型,并且您想要使用这两个名称空间中的类型;在这种情况下,“使用”两个命名空间可能会太混乱。此外,“使用”使扩展方法发挥作用;如果您不想绑定某些扩展方法,请不要包含包含它们的命名空间。
我不明白这个问题的措辞方式。我想也许你会问:
也就是说,为什么这不起作用:
在不同的文件中:
using Foo;
仅包含直接在 Foo 内声明的类型。它不会引入直接在 Foo 内部声明的命名空间。 C# 的设计者认为像这样将命名空间“纳入作用域”太令人困惑了;通常人们使用using
指令将类型“纳入范围”。The latter. You are using the "Text" namespace that is nested inside the "System" namespace.
Note that when you say
That is just a short form way of writing
Namespaces only have "simple" names.
Typically you do, yes. There are some situations in which you might want to not do so. For example, if you have two namespaces that both contain a type of the same name and you want to use types from both namespaces; in that case it might be too confusing to have a "using" of two namespaces. Also, "using" brings extension methods into play; if there are extension methods that you do not want to bind to, do not include the namespace that contains them.
I do not understand the question the way it is worded. I think maybe you are asking:
That is, why does this not work:
In a different file:
The
using Foo;
only includes the types declared directly inside Foo. It does not bring in namespaces declared directly inside Foo. The designers of C# thought that it was too confusing to bring namespaces "into scope" like this; typically people useusing
directives to bring types "into scope".总的来说,命名空间对于计算机的意义不是分层的。
System.Text
下的类型(例如System.Text.StringBuilder
)不被视为在System
中 -因此,如果您想要两者的类型,则需要单独列出它们:就编译器而言,唯一一次将它们视为层次结构 - 无论如何,我能想到 - 是在涉及命名空间时 声明某些内容:
您在以下位置
Test
,就好像您导入了Foo
、Foo.Bar
和Foo.Bar。巴兹。
当然,从人类理解的角度来看,命名空间确实形成了层次结构 - 这使得查找事物变得容易。但总的来说,编译器和运行时不使用事物的层次结构视图。
Namespaces are, by and large, not hierarchical in their meaning to the computer. The types under
System.Text
(e.g.System.Text.StringBuilder
) are not considered to be inSystem
- so you'd need to list them separately if you wanted types from both:The only time when they're considered as a hierarchy as far as the compiler is concerned - that I can think of, anyway - is when it comes to the namespace you declare something in:
Within
Test
, it's as if you've importedFoo
,Foo.Bar
andFoo.Bar.Baz
.Now of course namespaces do form hierarchies from a human understanding point of view - the make it easy to find things. But the compiler and runtime don't use that hierarchical view of things, by and large.
是的,或者完全限定类型名称。
两个命名空间的使用
是完全独立的。它们的名称暗示了一种关系,但从这些命名空间之外可以被认为是象征性的。
当您只有第一次使用时,您不能使用
System.Text
的成员。并且您不需要第一个即可使用第二个。
仅当您编写属于嵌套命名空间的代码时,您才会看到一些效果。
Yes, or fully qualify the type-names.
The 2 namespace usings
are totally independent. Their names suggest a relation but from outside those namespaces that can be considered symbolical.
You cannot use members of
System.Text
when you only have the first using.And you don't need the first in order to use the second.
Only when you write code that belongs to a nested namespace you see some effect.
using System.Text
导入System.Text
内的所有声明,但不导入其文件夹中的任何声明。没有像 Java 中那样的 System.Text.* 等价物,就像想象中的语言一样。处理命名空间的一个简单方法是正常编写代码并按 CTRL+。通过未定义的关键字告诉 VS.NET 自动为您添加
using
引用。using System.Text
imports all declarations insideSystem.Text
, but nothing in its folders. There's noSystem.Text.*
equivalent like in Javalike in an imaginary language.An easy way to deal with namespaces is to just write your code normally and CTRL+. over undefined keywords to tell VS.NET to automatically add the
using
reference for you.您正在访问嵌套在系统命名空间内的文本命名空间。是的,您需要包含您使用的每个名称空间。
命名空间结构可能如下所示:
You are accessing the Text Namespace nested inside the System Namespace. And yes, you need to include every namespace you use.
A namespace structure can look something like the following: