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.
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.
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;
发布评论
评论(7)
我只会在命名空间冲突的情况下使用别名(即仅当我不得不时)。
至少对我来说,任何其他用途都会令人困惑和分散注意力。
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.
命名空间别名并不是大多数代码库的共同特征 - 当我最后一次使用它时,高级开发人员对它并不熟悉,尽管他已经使用 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:
就我个人而言,我只会用它来保持智能感知干净。
如果您重命名类型,则会为维护程序员打开潘多拉魔盒。
Personally, I would only use it to keep intelli-sense clean.
If you rename types you open up Pandora's box for maintenance programmers.
命名空间别名的两种最常见的情况是:
对于第一种情况,没有评论。对于第二个,一个很好的例子是从 C 移植代码并使用命名空间别名以实现实用性,例如:
即使您将上述别名视为懒惰编程,它们也有助于避免错误。
The two most common cases for namespace aliases are:
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:
Even if you consider the above aliases as lazy programming, they help to avoid mistakes.
我认为那些倾向于根本不使用它们的人正在给出了您所要求的答案。这就是惯例。这并不是说它没有用,但不使用它仍然是常态。
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.
您可以采用约定来使用它们进行抽象。
这样,您可以轻松地
在某个时刻替换该行,而无需更改其他代码(假设您没有通过抽象来创建对实际类型的依赖关系......)
You could adopt a convention to use them for abstraction.
This way you can easily just replace that line with
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...)
我通常只在需要在不同命名空间中使用两个类似命名的类并且不想在代码中完全指定类型时才使用它们:
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: