如何解决类型和对象之间常见的命名冲突?

发布于 2024-08-25 16:16:21 字数 668 浏览 3 评论 0原文

由于标准 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 技术交流群。

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

发布评论

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

评论(5

深居我梦 2024-09-01 16:16:21

类型和属性具有完全相同的名称并不罕见。是的,这看起来有点奇怪,但不可否认的是,重命名属性以避免这种冲突看起来更奇怪。

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.

绅刃 2024-09-01 16:16:21

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.

为人所爱 2024-09-01 16:16:21

老实说,我对此没有意见——如果您的静态方法/成员在名称和目的上不是明显静态的,那么您会遇到比名称冲突更大的问题。

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.

兔小萌 2024-09-01 16:16:21

我不认为这对我造成过任何问题。以下是属性的一般约定。唯一有用的可能是习惯这些......

  • Pascal Case,没有下划线。
  • 尽量避免缩写。
  • 成员的差异必须大于大小写
    可从不区分大小写使用
    Visual Basic .NET 等语言。

原因:这个约定是一致的
与 .NET Framework 一起使用,并且很容易
读。喜欢

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 be getting used to these....

  • Pascal Case, no underscores.
  • Try to avoid abbreviations.
  • Members must differ by more than case
    to be usable from case-insensitive
    languages like Visual Basic .NET.

Why: This convention is consistent
with the .NET Framework and is easy to
read. like

public int RecordId

reference : NET Programming Standards and Naming Conventions

also check this: General Naming Conventions

故事灯 2024-09-01 16:16:21

我会说,我不介意碰撞,因为编译器可以解决它。 但是,本着提供我见过的其他解决方案并让其他人自己决定的精神......我认为这就是(对我来说)使用 My* 的丑陋模式出现的地方。

例如:

public class Foo { /* ... */ }

public class Bar
{
    public Foo MyFoo { get; set; }
    // ...
}

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:

public class Foo { /* ... */ }

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