如何解决类型和对象之间常见的命名冲突?
由于标准 C# 约定是将公共属性的首字母大写,旧的 C++ 约定(类型名称首字母大写,非类型名称首字母小写)并不能防止经典名称冲突,即最明显的对象名称与类型匹配name:
class FooManager
{
public BarManager BarManager { get; set; } // Feels very wrong.
// Recommended naming convention?
public int DoIt()
{
// 1st and 2nd Bar Manager are different symbols
return BarManager.Blarb + BarManager.StaticBlarb;
}
}
class BarManager
{
public int Blarb { get; set; }
public static int StaticBlarb { get; set; }
}
似乎可以编译,但感觉很不对劲。是否有推荐的命名约定来避免这种情况?
Since the standard c# convention is to capitalize the first letter of public properties, the old c++ convention of initial capital for type names, and initial lowercase for non-type names does not prevent the classic name collision where the most obvious object name matches the type name:
class FooManager
{
public BarManager BarManager { get; set; } // Feels very wrong.
// Recommended naming convention?
public int DoIt()
{
// 1st and 2nd Bar Manager are different symbols
return BarManager.Blarb + BarManager.StaticBlarb;
}
}
class BarManager
{
public int Blarb { get; set; }
public static int StaticBlarb { get; set; }
}
It seems to compile, but feels so wrong. Is there a recommend naming convention to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
类型和属性具有完全相同的名称并不罕见。是的,这看起来有点奇怪,但不可否认的是,重命名属性以避免这种冲突看起来更奇怪。
Eric Lippert 有关于这个主题的博文 。
然而,对于编译器来说没有任何歧义。
Having a type and a property with the exact same name isn't uncommon. Yes, it looks a little weird, but renaming properties to avoid this clash looks even weirder, admittedly.
Eric Lippert had a blog post on this exact topic.
There is no ambiguity for the compiler, however.
C# 约定是以与命名类相同的方式命名属性。你之所以觉得这是错误的,是因为你来自不同的背景。但如果你使用 if 一段时间,你就会发现它不会给你带来任何问题,而且当你习惯了它时,它会感觉很自然。没有任何地方会以任何方式发生碰撞。我(作为 ac# 开发人员)觉得属性首字母小写的约定感觉不对。你只需要习惯它。
The c# convention is to name properties in the same way as you name your classes. The reason you feel it's wrong is because you come from a different background. But if you use if for a while you will learn that it doesn't cause you any problems and it will feel pretty natural when you are used to it. There is no place when it will collide in any way. I (as a c# developer) feel that the convention of initial lower case letter for properties feel wrong. You just have to get used to it.
老实说,我对此没有意见——如果您的静态方法/成员在名称和目的上不是明显静态的,那么您会遇到比名称冲突更大的问题。
I'm okay with this honestly -- if your static methods/members aren't obviously static by name and by purpose, you've got bigger problems than name collision.
我不认为这对我造成过任何问题。以下是属性的一般约定。唯一
有用的
可能是习惯
这些......可从不区分大小写使用
Visual Basic .NET 等语言。
public int RecordId
参考:NET 编程标准和命名约定
另请检查:一般命名约定
I do not think it has ever caused an issue for me. Following are general conventions, for Properties. Only thing
helpful
could begetting used
to these....to be usable from case-insensitive
languages like Visual Basic .NET.
public int RecordId
reference : NET Programming Standards and Naming Conventions
also check this: General Naming Conventions
我会说,我不介意碰撞,因为编译器可以解决它。 但是,本着提供我见过的其他解决方案并让其他人自己决定的精神......我认为这就是(对我来说)使用 My* 的丑陋模式出现的地方。
例如:
I will caviat this by saying, I don't mind the collision, as the compiler can work it out. However, in the spirit of offering other solutions I have seen, and letting others decide for myself... I think this is where the ugly (to me) pattern of using My* arose.
For instance: