System.Collections是“System名称空间的名称空间”吗?

时间:2022-10-09 20:17:34

Okay, so I've been reading a book on C# and .NET, and while learning about the DateTime structure and the following code:

好的,所以我一直在读一本关于C#和.NET的书,在学习DateTime结构和以下代码时:

DateTime dt = new DateTime(2015, 10, 17);

I asked myself "why didn't I have to reference it at the top by writing using System.DateTime"?

我问自己“为什么我不能通过使用System.DateTime编写来在顶部引用它”?

So I realized that since DateTime is a structure, and the "using" keyword is used to reference types (type being a general term to refer to a member from the set {class, interface, structure, enumeration, delegate}), defined in a particular namespace (in this case, DateTime is of type structure from the System Namespace).

所以我意识到,因为DateTime是一个结构,并且“using”关键字用于引用类型(类型是一个通用术语,用于引用集合{class,interface,structure,enumeration,delegate}中的成员),在特定命名空间(在本例中,DateTime是System Namespace中的类型结构)。

But I noticed that at the top of my file, the following using directives are found (they were put there automatically when I created my C# console application):

但是我注意到在我的文件顶部,找到了以下using指令(当我创建我的C#控制台应用程序时它们被自动放在那里):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

I was wondering why the using directives that follow using System were necessary, when aren't they all under the System namespace and therefore they should already be referenced?

我想知道为什么使用System后面的using指令是必要的,何时它们都不在System命名空间下,因此它们应该已被引用?

So I'm guessing it's because all the ones declared below it are namespaces, as opposed to a type from the set {class, interface, structure, enumeration, delegate}. But would it be wrong to refer to them as "namespaces OF (or belonging to) the namespace System," and if so, what is the reason that you must explicitly reference namespaces of a namespace instead of them being implicitly included under using System?

所以我猜它是因为它下面声明的所有都是名称空间,而不是来自集合{class,interface,structure,enumeration,delegate}的类型。但是将它们称为“命名空间系统的命名空间(或属于命名空间系统)”是否错误?如果是这样,那么您必须显式引用命名空间的命名空间而不是使用系统隐式包含它们的原因是什么?

I hope my question was clear but if not, please let me know so I can edit it.

我希望我的问题很明确,但如果没有,请告诉我,以便我可以编辑。

4 个解决方案

#1


24  

Internally, all .net types always have full type names;

在内部,所有.net类型始终具有完整的类型名称;

System.DateTime
System.Collections.Generic.Dictionary<string, int>
System.Threading.Tasks.Task

what the 'usings' do is to let you refer 'invisibly' to everything before a '.' character. So when you write

“使用”的作用是让你在''之前'无形地'引用一切。字符。所以当你写作

DateTime

C# will try

C#会试试

System.DateTime
System.Collections.Generic.DateTime
System.Linq.DateTime
System.Text.DateTime
etc...

Since only one exists (System.DateTime) it's an unambiguous match, and C# knows what you're talking about.

因为只存在一个(System.DateTime),所以它是一个明确的匹配,而C#知道你在说什么。

If there were two matches, you'd get a compiler error about the ambiguity. So if there were a System.Threading.Task.DateTime, your program wouldn't compile.

如果有两个匹配项,则会出现关于歧义的编译器错误。因此,如果有System.Threading.Task.DateTime,您的程序将无法编译。

All this sets the scene for the answer to your question. If

所有这些都为你的问题的答案设定了场景。如果

using System;

included everything below it, then it would be hard to ensure that things aren't ambiguous. For example, if you wanted to differentiate between two theoretical classes

包括它下面的一切,然后很难确保事情不含糊。例如,如果您想区分两个理论类

System.WebPages.Button
System.Desktop.Button

then a plain old using System wouldn't play nice with new Button().

然后一个普通的旧使用系统将不会很好地使用新的Button()。

#2


3  

The reason is that if it weren't that way, you'd have no way of avoiding unwanted name collisions in certain circumstances. For instance, if you had a type Baz in Foo namespace and a type Baz in Foo.Bar namespace, you'd always have to fully-qualify your types, e.g. Foo.Baz or Foo.Bar.Baz, in your code, which would make the using directives pointless.

原因是,如果不是这样,在某些情况下你就无法避免不必要的名称冲突。例如,如果你在Foo命名空间中有一个类型Baz,在Foo.Bar命名空间中有一个类型Baz,你总是必须完全限定你的类型,例如你的代码中的Foo.Baz或Foo.Bar.Baz会使using指令毫无意义。

Given the way it is, you only have to deal with name collisions if you actually have using directives for both namespaces that contain your type.

鉴于它的方式,如果你实际上对包含你的类型的两个命名空间都使用了指令,你只需要处理名称冲突。

#3


2  

You don't have to have other using directives, e.g. if you wanted a List you could just declare in your code:

您不必拥有其他使用指令,例如如果你想要一个List,你可以在你的代码中声明:

System.Collections.Generic.List<String> listOfStrings = new System.Collections.Generic.List<String>;

To create a new List, but the using statement lets you simply declare it as:

要创建新的List,但using语句允许您将其声明为:

List<String> listOfStrings = new List<String>;

The reason you can't do the latter with only:

你不能只用后者做后者的原因是:

using System;

Is because there could be a class in System called List, as well as the List class in System.Collections.Generic. If so, when you create a new list object, how does it know which List class you're referring to?

是因为System中可能有一个名为List的类,以及System.Collections.Generic中的List类。如果是这样,当您创建新的列表对象时,它如何知道您所指的List类?

So essentially they're used to prevent naming conflicts, and to enable programmers to group things into logically named groups, e.g. input and output related classes can be found in System.IO

因此,它们基本上用于防止命名冲突,并使程序员能够将事物分组为逻辑命名的组,例如输入和输出相关的类可以在System.IO中找到

#4


1  

Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.

创建using指令以使用命名空间中的类型,而无需指定命名空间。 using指令不允许您访问嵌套在指定名称空间中的任何名称空间。

from https://msdn.microsoft.com/en-us/library/sf0df423.aspx

来自https://msdn.microsoft.com/en-us/library/sf0df423.aspx

The same thing happens when I am assigned namespaces cause of my folder structure like projectName\Code\DBFiles if I do using Code; on one of my controllers it doesn't give me access classes in DBFiles that's why I set all my class name spaces to the root namespace.

如果我使用Code,当我为我的文件夹结构分配命名空间时会发生同样的事情,例如projectName \ Code \ DBFiles;在我的一个控制器上,它没有给我DBFiles中的访问类,这就是我将所有类名空间设置为根命名空间的原因。

#1


24  

Internally, all .net types always have full type names;

在内部,所有.net类型始终具有完整的类型名称;

System.DateTime
System.Collections.Generic.Dictionary<string, int>
System.Threading.Tasks.Task

what the 'usings' do is to let you refer 'invisibly' to everything before a '.' character. So when you write

“使用”的作用是让你在''之前'无形地'引用一切。字符。所以当你写作

DateTime

C# will try

C#会试试

System.DateTime
System.Collections.Generic.DateTime
System.Linq.DateTime
System.Text.DateTime
etc...

Since only one exists (System.DateTime) it's an unambiguous match, and C# knows what you're talking about.

因为只存在一个(System.DateTime),所以它是一个明确的匹配,而C#知道你在说什么。

If there were two matches, you'd get a compiler error about the ambiguity. So if there were a System.Threading.Task.DateTime, your program wouldn't compile.

如果有两个匹配项,则会出现关于歧义的编译器错误。因此,如果有System.Threading.Task.DateTime,您的程序将无法编译。

All this sets the scene for the answer to your question. If

所有这些都为你的问题的答案设定了场景。如果

using System;

included everything below it, then it would be hard to ensure that things aren't ambiguous. For example, if you wanted to differentiate between two theoretical classes

包括它下面的一切,然后很难确保事情不含糊。例如,如果您想区分两个理论类

System.WebPages.Button
System.Desktop.Button

then a plain old using System wouldn't play nice with new Button().

然后一个普通的旧使用系统将不会很好地使用新的Button()。

#2


3  

The reason is that if it weren't that way, you'd have no way of avoiding unwanted name collisions in certain circumstances. For instance, if you had a type Baz in Foo namespace and a type Baz in Foo.Bar namespace, you'd always have to fully-qualify your types, e.g. Foo.Baz or Foo.Bar.Baz, in your code, which would make the using directives pointless.

原因是,如果不是这样,在某些情况下你就无法避免不必要的名称冲突。例如,如果你在Foo命名空间中有一个类型Baz,在Foo.Bar命名空间中有一个类型Baz,你总是必须完全限定你的类型,例如你的代码中的Foo.Baz或Foo.Bar.Baz会使using指令毫无意义。

Given the way it is, you only have to deal with name collisions if you actually have using directives for both namespaces that contain your type.

鉴于它的方式,如果你实际上对包含你的类型的两个命名空间都使用了指令,你只需要处理名称冲突。

#3


2  

You don't have to have other using directives, e.g. if you wanted a List you could just declare in your code:

您不必拥有其他使用指令,例如如果你想要一个List,你可以在你的代码中声明:

System.Collections.Generic.List<String> listOfStrings = new System.Collections.Generic.List<String>;

To create a new List, but the using statement lets you simply declare it as:

要创建新的List,但using语句允许您将其声明为:

List<String> listOfStrings = new List<String>;

The reason you can't do the latter with only:

你不能只用后者做后者的原因是:

using System;

Is because there could be a class in System called List, as well as the List class in System.Collections.Generic. If so, when you create a new list object, how does it know which List class you're referring to?

是因为System中可能有一个名为List的类,以及System.Collections.Generic中的List类。如果是这样,当您创建新的列表对象时,它如何知道您所指的List类?

So essentially they're used to prevent naming conflicts, and to enable programmers to group things into logically named groups, e.g. input and output related classes can be found in System.IO

因此,它们基本上用于防止命名冲突,并使程序员能够将事物分组为逻辑命名的组,例如输入和输出相关的类可以在System.IO中找到

#4


1  

Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.

创建using指令以使用命名空间中的类型,而无需指定命名空间。 using指令不允许您访问嵌套在指定名称空间中的任何名称空间。

from https://msdn.microsoft.com/en-us/library/sf0df423.aspx

来自https://msdn.microsoft.com/en-us/library/sf0df423.aspx

The same thing happens when I am assigned namespaces cause of my folder structure like projectName\Code\DBFiles if I do using Code; on one of my controllers it doesn't give me access classes in DBFiles that's why I set all my class name spaces to the root namespace.

如果我使用Code,当我为我的文件夹结构分配命名空间时会发生同样的事情,例如projectName \ Code \ DBFiles;在我的一个控制器上,它没有给我DBFiles中的访问类,这就是我将所有类名空间设置为根命名空间的原因。