目录中的 C# 类在其命名空间中没有目录名称

发布于 2024-09-02 15:04:04 字数 315 浏览 3 评论 0原文

如果您在 Visual Studio 项目中添加一个目录,并在其中添加一个,则命名空间遵循包含该目录的整个路径

但有时,我更喜欢将类放在主项目命名空间中,尽管它位于目录结构中,只是因为我不想让我的代码变得混乱。 我经常将 Myproject.MyDirectory 命名空间重写为 Myproject

您认为可以吗?或者是否有任何约定规定目录中的每个类都必须将其包含在命名空间中?

谢谢

If you add a directory in your Visual Studio project and you add a class inside it, the namespace will respect the whole path the directory inclusive.

But sometimes, I prefer having the class in the main project namespace, although it lies in a directory structure, just because I don't want to have mess in my code.
So often happens that I rewrite the Myproject.MyDirectory namespace to be Myproject only.

Is it OK in your opinion? Or does any convention say that every class inside the directory must have it included in the namespace ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下客 2024-09-09 15:04:04

在我看来,文件夹和命名空间有不同的用途。

文件夹有助于为阅读您的代码的人员提供清晰的层次结构

。命名空间有助于为使用您的代码的人员提供清晰的层次结构。例如,当他们看不到实际的源代码时,调用您的代码提供的 API。

因此,只要各有道理,不一致也是完全可以的。

In my view, folders and namespaces serve for different purposes.

Folders are useful to provide a clear hierarchy for people who read your code

Namespaces are useful to provide a clear hierarchy for people who use your code. E.g. calling the API provided by your code, when they don't see the actual source code.

Therefore, inconsistency is perfectly fine, as long as each makes sense.

本宫微胖 2024-09-09 15:04:04

没有任何约定限制您想要做的事情。我的项目中通常有多个目录来组织类文件并使用不同的命名空间,而不考虑目录结构,并且我没有任何问题。

There isn't any convention restricting from what you're trying to do. I usually have multiple directories in my project to organize class files and use different namespaces with no respect to the directory structure and I don't have any problems.

浅听莫相离 2024-09-09 15:04:04

没有限制,但有些人认为命名空间与路径相同很有用。因此,您可以选择任何命名空间并将类文件放置在您想要的任何位置。

There is no constraint, but some think it's useful to have the namespace identical to the path. So you could choose any namespace and place the class file wherever you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文