比较和比较C ++与C#名称空间的层次结构(Comparing and Contrasting C++ vs C# namespace hierarchies)

编程入门 行业动态 更新时间:2024-10-27 23:22:52
比较和比较C ++与C#名称空间的层次结构(Comparing and Contrasting C++ vs C# namespace hierarchies)

我知道C#支持命名空间嵌套,就像C ++一样,它们都允许看起来像这样的代码...

namespace A { namespace B { ... } }

来自C ++背景并深入到C#世界中,我一直在思考如何通过必须发布的所有使用指令来展现组件的层次嵌套,例如,

using System.Collections; using System.Collections.Generic;

我确信微软希望并设计这些库在逻辑上是分级组织的,但是如果没有看到源的帮助,我无法验证System.Collections.Generic是否具有Generic作为Collections的嵌套命名空间,但我认为它是,它是通过名称空间嵌套完成的,就像用A和B看到的一样。现在,一旦我开始在自己的源代码中完成某些事情并且声明代码如下所示

namespace C.D { ... }

关于层次结构,我到底在这里做了什么? 我是在代码中引入一个单独的名称空间标识符“CD”,其中“。” 只是一种友好的方式,建议一个层次结构,可能存在也可能不存在取决于代码结构,或者我隐式地声明两个命名空间“C”和“D”D嵌套在C? 在为我们的C#代码库编写一个DevelopmentApplications命名空间时,我遇到了这个问题,该代码旨在严格包含所有用于增强我们的软件以帮助其开发的开发工具。 在那些工具中,我从来没有宣布独立的封闭命名空间DevelopmentApplications(这是我在C ++中必须做的事情)...

namespace DevelopmentApplications { ... }

...但总是创建类似的应用程序

namespace DevelopmentApplications.MyDevelopmentApp { ... }

我知道这个领域对于一些人来说是一个混乱的原因,因为作者正在努力理解Foo.Bar.Baz和Foo.Bar之间的关系。 还有一个C#开发人员正在进入C ++领域的反问题 ,它提供了一些有关此问题的信息。

我认为另一种方式来陈述这个问题是,在C ++中使用'::'操作符来完全限定一个类型,我知道确保类型声明的代码嵌套在某些名称空间层次结构中。 但在C#中使用'。' 运算符要完全限定某种类型,这种类型也必须存在于某些深层嵌套的命名空间层次结构中? 我在这里假定C#使用像ABC这样的名称空间不一定需要AB和C之间的层次关系,或者AB或C甚至作为单独的名称空间存在。

如果有人能够找到或者知道关于这个语法的相关语言规范,我很乐意阅读它。

I know C# supports namespace nestings, as does C++, which both allow code that looks like this...

namespace A { namespace B { ... } }

Coming from a C++ background and diving into the C# world I've been meditating upon what appears as a hierarchical nesting of components via all the using directives that must be issued to exploit .NET, eg

using System.Collections; using System.Collections.Generic;

I'm sure Microsoft intended and designed these libraries to be logically hierarchically organized, but without the aid of seeing the source I cannot verify if System.Collections.Generic has Generic as a nested namespace of Collections, but I assume it is, and that it was accomplished with namespace nestings like seen with A and B. Now once I start cooking up things in my own source and declaring code that looks like this

namespace C.D { ... }

what exactly am I achieving here with respect to a hierarchy? Am I introducing to the code a singular namespace identifier "C.D" where the '.' is simply a friendly means of suggesting a hierarchy that may or may not exist depending upon the code structure or am I implicitly declaring two namespaces "C" and "D" with D nested within C? I've come across this question while cooking up a DevelopmentApplications namespace to our C# codebase that is meant to strictly contain all development tools used to augment our software to aid in its development. In those tools I've never declared a standalone enclosing namespace DevelopmentApplications (which is something I would HAVE to do in C++)...

namespace DevelopmentApplications { ... }

...but instead always create applications that go like

namespace DevelopmentApplications.MyDevelopmentApp { ... }

I know this area is a cause of confusion for some because of the following question where the author is struggling to understand the relationship between Foo.Bar.Baz and Foo.Bar. There's also an inverse question of a C# developer entering C++ land that gives some insight into this issue.

I suppose another way to state the question is that in C++ using the '::' operator to fully qualify a type I know guarantees that the code that type was declared in is nested deep in some namespace hierarchy. But in C# using the '.' operator to fully qualify some type must that type also exist in some deeply nested namespace hierarchy? I'm assuming here that C#'s use of a namespace like A.B.C does not necessarily require a hierarchical relationship between A B and C or that A B or C even exist as individual namespaces.

If someone can find or knows the relevant language specification regarding this syntax I'd love to read it.

最满意答案

C#4.0规范的第9.2节指出:

名称空间声明合格标识符可以是单个标识符或由“。”标记分隔的一系列标识符。 后一种形式允许程序定义一个嵌套名称空间,而不用词汇嵌套几个名称空间声明。 例如,

namespace N1.N2 { class A {} class B {} }

在语义上等同于

namespace N1 { namespace N2 { class A {} class B {} } }

你说:

我在这里假定C#使用像ABC这样的名称空间不一定需要AB和C之间的层次关系,或者AB或C甚至作为单独的名称空间存在。

这个假设是错误的。 名称空间ABC必须包含全局名称空间中的名称空间A ,名称空间A中的名称空间B以及名称空间B中的名称空间C

Section 9.2 of the C# 4.0 specs states:

The qualified-identifier of a namespace-declaration may be a single identifier or a sequence of identifiers separated by “.” tokens. The latter form permits a program to define a nested namespace without lexically nesting several namespace declarations. For example,

namespace N1.N2 { class A {} class B {} }

is semantically equivalent to

namespace N1 { namespace N2 { class A {} class B {} } }

You state:

I'm assuming here that C#'s use of a namespace like A.B.C does not necessarily require a hierarchical relationship between A B and C or that A B or C even exist as individual namespaces.

That assumption is false. The namespace A.B.C necessarily involves namespace A in the global namespace, namespace B within namespace A, and namespace C in namespace B.

更多推荐

本文发布于:2023-07-14 23:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:层次   名称   结构   空间   hierarchies

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!