C# 中的类型/命名空间别名约定

发布于 2024-08-08 22:29:15 字数 1431 浏览 8 评论 0原文

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

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

发布评论

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

评论(7

假情假意假温柔 2024-08-15 22:29:15

我只会在命名空间冲突的情况下使用别名(即仅当我不得不时)。

至少对我来说,任何其他用途都会令人困惑和分散注意力。

I would only use aliases in the case of namespace conflict (i.e. only if I had to).

For me at least, any other use is just confusing and a distraction.

七堇年 2024-08-15 22:29:15

命名空间别名并不是大多数代码库的共同特征 - 当我最后一次使用它时,高级开发人员对它并不熟悉,尽管他已经使用 C# 多年了。

由于它很罕见,因此尚未为其制定惯例。

我想说,如果您要使用别名,请与您的团队讨论以创建您自己的约定。

我见过使用别名的几种不同方式:

  • 命名空间的缩写。通常是命名空间中的大写字母。
  • 命名空间的最末端 - 如果复数被取消复数。
  • 描述性短名称。

Namespace aliases are not a common feature of most code bases - the last place when I have used it, the senior developer was unfamiliar with it, though having worked with C# for many years.

Since it is rare, conventions have not been developed for it.

I would say that if you are going to use aliases, discuss this with your team to create your own convention.

Several different ways I have seen aliases used:

  • An acronym of the namespace. Usually the capitals in the namespace.
  • The very end of the namespace - if plural de-pluralized.
  • A descriptive short name.
猫九 2024-08-15 22:29:15

就我个人而言,我只会用它来保持智能感知干净。

using StringBuilder = System.Text.StringBuilder;

如果您重命名类型,则会为维护程序员打开潘多拉魔盒。

Personally, I would only use it to keep intelli-sense clean.

using StringBuilder = System.Text.StringBuilder;

If you rename types you open up Pandora's box for maintenance programmers.

陌上芳菲 2024-08-15 22:29:15

命名空间别名的两种最常见的情况是:

  1. 我看到它并使用它(作为一种趋势)
  2. 我从另一种语言移植代码

对于第一种情况,没有评论。对于第二个,一个很好的例子是从 C 移植代码并使用命名空间别名以实现实用性,例如:

using i64 = System.Int64;
using u8 = System.Byte;
using u32 = System.UInt32;
using u64 = System.UInt64;

即使您将上述别名视为懒惰编程,它们也有助于避免错误。

The two most common cases for namespace aliases are:

  1. I saw it and I use it (as a trend)
  2. I port code from another language

For the first case, no comment. For the second one a good example is porting code from C and use namespace aliases for practicality like:

using i64 = System.Int64;
using u8 = System.Byte;
using u32 = System.UInt32;
using u64 = System.UInt64;

Even if you consider the above aliases as lazy programming, they help to avoid mistakes.

李不 2024-08-15 22:29:15

我认为那些倾向于根本不使用它们的人正在给出了您所要求的答案。这就是惯例。这并不是说它没有用,但不使用它仍然是常态。

I think the people who are inclined toward not using them at all are giving the answer you asked for. That is the convention. This isn't to say that it can't be useful, but not using it remains the norm.

撞了怀 2024-08-15 22:29:15

您可以采用约定来使用它们进行抽象。

using Id = System.Int32;

这样,您可以轻松地

using Id = System.Guid;

在某个时刻替换该行,而无需更改其他代码(假设您没有通过抽象来创建对实际类型的依赖关系......)

You could adopt a convention to use them for abstraction.

using Id = System.Int32;

This way you can easily just replace that line with

using Id = System.Guid;

at some point and no other code would have to change (assuming you didn't peeked through the abstraction to create dependencies on the actual type...)

他不在意 2024-08-15 22:29:15

我通常只在需要在不同命名空间中使用两个类似命名的类并且不想在代码中完全指定类型时才使用它们:

using xItem = My.Project.NameSpace.Item;
using yItem = Your.ThirdParty.Framework.Item;

I generally only use them when I need to use two similarly named classes in different namespaces, and don't want to fully specify the type in the code:

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