简体   繁体   English

为什么“使用命名空间”指令是C#中接受的编码实践?

[英]Why “using namespace” directive is accepted coding practice in C#?

I am just curious to know why "using namespace" directive is acceptable in C#, though in C++ it is not . 我很想知道为什么“使用命名空间”指令在C#中是可以接受的, 尽管在C ++中它不是 I am aware that C++ and C# are different, but my guess is C++ and C# come almost from same family, and should be using the same ideas for namespace resolution. 我知道C ++和C#是不同的,但我的猜测是C ++和C#几乎来自同一个家族,并且应该使用相同的想法来进行命名空间解析。 C++ and C# both have an alias keyword to get around the namespace clash. C ++和C#都有一个别名关键字来绕过命名空间冲突。

Can anybody point me, what I am not reading between lines in C# that makes it acceptable to use "using namespace" directive, and avoid the problems that C++ cannot. 任何人都可以指出我,我不是在C#中的行之间阅读,这使得使用“using namespace”指令是可以接受的,并避免了C ++无法解决的问题。

In C++, if you write using namespace in a header, then it will be in effect for anyone who includes that header. 在C ++中,如果您在标头中using namespace编写,那么它将对包含该标头的任何人有效。 This makes it pretty much unusable in headers. 这使它在标题中几乎无法使用。 At that point, you might as well avoid it (at global scope) in .cpp files as well, if only for the sake of consistency, and to make moving implementations between .h and .cpp easier. 此时,您也可以在.cpp文件中避免它(在全局范围内),如果只是为了保持一致性,并使.h和.cpp之间的移动实现更容易。

(note that locally scoped using namespace - ie within a function - are generally considered fine; it's just that they don't help with verbosity much) (注意, using namespace本地作用域 - 即在函数内 - 通常被认为是好的;只是它们对冗长度没有帮助)

In C#, there is nothing like #include , and the scope of a using directive will never span beyond a single .cs file. 在C#中,没有像#include这样的东西,而using指令的范围永远不会超出单个.cs文件。 So it's pretty much safe to use everywhere. 所以到处使用它几乎是安全的。

The other reason is the design of the standard library. 另一个原因是标准库的设计。 In C++, you just have std (well, now also a few more underneath it, but they are rarely used). 在C ++中,你只有std (好吧,现在还有一些,但它们很少使用)。 In C#, you have gems such as System.Collections.Generic , which is extremely verbose for something that's used very commonly. 在C#中,你有像System.Collections.Generic这样的宝石,这对于非常常用的东西来说非常冗长。 It's just much more painful to avoid using in C# than it is in C++. 避免在C#中使用比在C ++中using要痛苦得多。

To sum it up, while C# and C++ do share some common design, on the matter of code modularity (I'd assign headers, modules, namespaces all to that group), their design is very different. 总而言之,虽然C#和C ++确实共享一些通用设计,但在代码模块化方面(我将头部,模块,名称空间全部分配给该组),他们的设计却截然不同。

For me, it comes down to the support tools. 对我来说,它归结为支持工具。 Intellisense, the quick class lookup (F1 key), and refactoring options of Visual Studio give the needed reference lookup functionality. Intellisense,快速类查找(F1键)和Visual Studio的重构选项提供了所需的参考查找功能。

Also, C# has every method within a class--there are no namespace level functions. 此外,C#具有类中的每个方法 - 没有命名空间级别的函数。

In general a difference of C# and C++ is how compilation units are handled and specified. 通常,C#和C ++的区别在于如何处理和指定编译单元。

C++ uses header files to publish class declarations, and needs a compilation unit where this class is implemented (defined). C ++使用头文件来发布类声明,并且需要一个实现(定义)此类的编译单元。 A using namespace <xxx> statement in header files is strongly discouraged practice for C++, because this can easily lead to namespace clashes and ambiguities when included from some client code. 对于C ++,强烈建议不要在头文件中using namespace <xxx>语句,因为当从某些客户端代码中包含时,这很容易导致命名空间冲突和歧义。 In your class declaration you should explicitly state what you want from other namespaces (including std ). 在类声明中,您应该从其他命名空间(包括std )明确说明您想要的内容。

C# has single compilation units which eases use of using namespace <xxx> statements a bit. C#有单个编译单元,可以轻松使用using namespace <xxx>语句。 Nevertheless I'd prefer aliasing imported namespaces, if you want to avoid tedious typing. 不过,如果你想避免繁琐的输入,我更喜欢别名导入的命名空间。 Placing using statements in a .cs file might cause ambiguous definitions as well. 在.cs文件中using语句也可能导致定义不明确。

While I completely agree with others that using namespaces should not be done in headers, I think banning them in cpp files is shortsighted. 虽然我完全同意其他人不应该在标题中使用命名空间,但我认为在cpp文件中禁止它们是短视的。 If you try to adhere to organizing declarations into namespaces 'nicely', but then ban the usage of 'using namespace' altogether, the path of least resistance for coders becomes the under use of namespaces. 如果你试图坚持将声明组织成'名称空间',但是然后完全禁止使用'using namespace',那么编码器阻力最小的路径就变成了名称空间的使用不足。

For instance, in the above post by Pavel Minaev, he rightfully points out the namespace difference between a common C++ namespace, 'std', and a C# namespace, 'System.Collections.Generic'. 例如,在Pavel Minaev的上述文章中,他理所当然地指出了公共C ++名称空间“std”和C#名称空间“System.Collections.Generic”之间的名称空间差异。 If you stop to think of why they are this way, a clear answer IMO is that C++ culture frowns on using namespace, while C# does not, so in C# you accept more verbose namespaces because they are not inherently painful to use. 如果你停下来思考它们为什么会这样,一个明确的答案IMO是C ++文化对使用命名空间不满意,而C#没有,所以在C#中你接受更详细的命名空间,因为它们本身并不痛苦。 I believe namespace organization in C# is much better than C++ largely because of this cultural difference, and better class organization and general readability are not trivial things. 我认为C#中的命名空间组织比C ++好得多,主要是因为这种文化差异,更好的类组织和一般可读性并不是一件容易的事情。

To put a different way, think about what would happen to people's file organization habits if an application required them to type fully qualified paths to load a file. 换句话说,如果应用程序要求他们键入完全限定的路径来加载文件,请考虑人们的文件组织习惯会发生什么。 They'd more likely just shove everything into a root folder to avoid the typing, not a good way to promote quality organization. 他们更可能只是将所有东西都推到根文件夹中以避免打字,而不是促进高质量组织的好方法。

While certainly not as clean as C#'s using directive, using namespace in cpp files is an overall win. 虽然肯定不像C#的using指令那样干净,但在cpp文件中使用命名空间是一个全面的胜利。

Unless you're a language purist, it saves time and makes coding easier. 除非你是语言纯粹主义者,否则它可以节省时间并使编码更容易。 Unless you're dealing with complicated systems of namespaces, it's perfectly acceptable. 除非你正在处理复杂的命名空间系统,否则它是完全可以接受的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#在嵌套命名空间中使用namespace指令 - C# using namespace directive in nested namespaces 在c#中使用命名空间 - using a namespace in c# 命名空间错误中不存在C#.NET命名空间名称 - 仅当使用在本地命名空间指令之外时 - 为什么? - C#.NET Namespace name does not exist in namespace error - only when using is outside local namespace directive - why? C#:找不到类型或名称空间名称“类”(是否缺少程序集引用的using指令?) - C#: The type or namespace name 'class' could not be found(are you missing a using directive for an assembly reference?) 在 C# 中使用系统指令时找不到类型或命名空间名称“文本” - The type or namespace name 'Text' could not be found when using System directive is active in C# 找不到类型或名称空间名称“ MySql”(您是否缺少using指令或程序集引用?)c#VS 2013 - The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference?) c# VS 2013 c#错误1找不到类型或名称空间名称”(是否缺少using指令或程序集引用?) - c# Error 1 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?) 如何在View中使用带有using指令的C#Global命名空间运算符? - How do I use the C# Global namespace operator with a using directive in my View? C#:找不到类型或名称空间名称“?Attribute”(是否缺少using指令或程序集引用? - C#:The type or namespace name '?Attribute' could not be found (are you missing a using directive or an assembly reference? 找不到类型或命名空间名称“ScriptName”(您是否缺少 using 指令或程序集引用?)修复 unity c# - The type or namespace name 'ScriptName' could not be found (are you missing a using directive or an assembly reference?) fix unity c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM