简体   繁体   English

命名类和名称空间是否相同?

[英]Naming classes and namespaces the same?

I'm a beginner on C# (I know Java well though) and came to a problem with naming namespaces and classes/interfaces the same. 我是C#的初学者(尽管我很了解Java),但在命名空间和类/接口命名相同时遇到了问题。

What won't work is this: 这是行不通的:

- Projekt
--> (Interface) Node
--> (Namespace/Folder) Node
-----> (Class): SomethingNode : Node
-----> ... 

Because there is an error that the type "Project.Node" and the Namespace "Project.Node" are named the same. 因为存在错误,所以类型“ Project.Node”和命名空间“ Project.Node”被命名为相同。

I will explain why I would intentionally like to name those the same: I have many classes who implement the interface "Node". 我将解释为什么有意命名相同的名称:我有许多实现接口“ Node”的类。 Those classes should all be in one namespace, so why don't name them "Node" I thought. 这些类都应该在一个名称空间中,所以我想为什么不将它们命名为“ Node”。 The Interface "Node" should be in the project's root namespace, because it's used by other classes and I don't want to eg move the interface "Node" to "Project.Node.Node", because I think it would be silly to allways import "Project.Node" just because I need the interface. 接口“ Node”应该在项目的根名称空间中,因为它被其他类使用,并且我不想例如将接口“ Node”移动到“ Project.Node.Node”,因为我认为这样做很愚蠢。总是因为我需要接口而导入“ Project.Node”。

So my question is, how should I resolve this situation? 所以我的问题是,我该如何解决这种情况? How should I name my classes/namespaces, is there a nice elegant way of doing this, are there any naming conventions for this kind of situation? 我应该如何命名我的类/命名空间,是否有一种很好的优雅方法,是否有这种情况的命名约定?

You should follow .NET conventions and name your interface INode . 您应该遵循.NET约定并命名您的接口INode Then classes inside the Node namespace will implement your INode interface. 然后, Node名称空间中的类将实现INode接口。

Easiest solution to the problem is simply to adopt the C#/.NET naming convention for interfaces, which is to add an I - INode , not the Java-esque Node . 解决此问题的最简单方法就是为接口采用C#/。NET命名约定,即添加I- INode而不是Java式Node Also, it SHOULD sit under the Nodes namespace. 另外,它应该位于Nodes名称空间下。 Importing a namespace isn't a cumbersome action, especially as most IDEs, and certainly Visual Studio, can automate it for you. 导入名称空间并不是一件繁琐的事情,特别是因为大多数IDE(当然还有Visual Studio)都可以为您自动化它。 It prevent your root namespace from being crowded and unbrowsable. 它可以防止您的根名称空间拥挤和不可浏览。

Secondly, I think that a more intuitive name for a namespace is using the plural: Projekt.Nodes , so you'll have this: 其次,我认为命名空间的更直观的名称是使用复数: Projekt.Nodes ,因此您将获得:

namespace Projekt
{
    namespace Nodes
    {
        public interface INode { }
        public class Node : INode { }
    }
}

Basically namespaces in .Net are very similar to packages in Java. 基本上,.Net中的名称空间与Java中的程序包非常相似。 You could get around this by calling your Interface INode. 您可以通过调用Interface INode来解决此问题。 It is a .Net convention to prefix an interface with the letter I. Silly, Hungarian notation, but silly conventions are what .Net developers are used to anyway ;-) .Net约定以字母I开头是一个接口。愚蠢的匈牙利符号,但是.Net开发人员习惯使用愚蠢的约定;-)

You are not allowed to have name collisions between types and namespaces. 不允许类型和名称空间之间发生名称冲突。 Then there's the .Net convention of prefixing interfaces with a capital 'i', so your interface Node would then become INode . 然后是.Net约定,使用大写的'i'作为接口的前缀,因此您的接口Node将成为INode This is a standard among all libraries and components of the .Net framework. 这是.Net框架的所有库和组件中的标准。 Applying the standard to your code should easily solve your problem of collisions. 将标准应用到您的代码应该可以轻松解决冲突问题。

Now in my opinion, from an architectual point of view, if all implementations of INode are under the Node namespace, I'd also expect to find INode under it as well. 现在,我认为,从体系结构的角度来看,如果INode所有实现都在Node命名空间下,那么我也希望在其下找到INode

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM