嵌套枚举和属性命名冲突 [英] Nested Enum and Property Naming Conflicts

查看:354
本文介绍了嵌套枚举和属性命名冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些相关的问题,这里和<一href="http://stackoverflow.com/questions/495051/c-naming-convention-for-enum-and-matching-property">here,但他们并没有真正给我满意的答案。问题是,枚举嵌套在一个类在C#中不能有相同的名称作为类的属性。我的例子:

 公共类卡
{
    公共枚举套装
    {
        俱乐部,
        钻石,
        黑桃,
        心
    }

    公共枚举排名
    {
        二,
        三,
        ...
        王,
        高手
    }

    公共西装套装{获得;私定; }
    公共秩秩{获得;私定; }
    ...
}
 

有几个选项破解解决这个问题,但他们似乎没有我的权利。

我可以移动的枚举类外,但你只想说套装而不是 Card.Suit ,这似乎是我错了。什么是套装 A 的范围之外?

我可以移动它们的类外,并将其更改为类似 CardSuit CardRank ,但后来我ð觉得我烤的上下文信息到枚举的时候,应该由一个类或命名空间名称进行处理的名称。

我可以改变枚举的名称西装排名,但这违背的Microsoft's命名指南。它只是感觉不对。

我可以改变属性名称。但是,为了什么呢?感觉直观的权利我想说套装= Card.Suit.Spades

我可以将枚举到仅包含这些枚举一个单独的静态类名为 CardInfo 。如果我不能想出别的,我觉得这是最好的选择。

所以,我想知道其他人在类似情况下所做的那样。这也将是不错的知道为什么这是不允许的。也许埃里克利珀还是有人可以在禁止它的决定附和?现在看来似乎只是引起混淆的的类,这可能通过强制使用 this.Suit 的属性名称来解决。 (类似当地人和成员之间的歧义。)我认为这是忽略了由于每一个功能开始-100分的事情,但我会好奇解决此讨论。

解决方案
  

这也将是不错的知道为什么这是不允许的。也许埃里克利珀还是有人可以在禁止它的决定附和?

该规则的一点是要确保查找时的名称还有就是类中没有歧义。的code某些地区被指定为定义的声明空间。声明空间的基本规则是在相同的声明空间中声明没有两个事物具有相同的名称(除的方法,它必须通过的签名的不同,不是的的名字的。)

使此规则的例外只是使事情变得更加混乱,而不是更少混乱。我同意这是伤脑筋,你不能有一个属性,并在同一个声明空间中声明的同名枚举,但一旦你开始做的异常则只会越来越成为一个烂摊子。它通常是一个不错的属性名称的唯一的标识方法组,类型参数,属性等。

请注意,此规则适用于事物的声明的声明中的空间,而不是事情的使用的一个声明空间。这是完全合法的,说:公共西装套装{获取;集;}规定该类型的诉讼不是在同一个声明空间与属性声明。当有人说Suit.X,搞清楚X是否在类型(即,X是一个静态成员)或属性(即X是一个实例成员)是有点棘手。见我对我们如何做到这一点的详细信息的文章:

<一个href="http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx">http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx

There are some related questions here and here, but they didn't really give me satisfactory answers. The problem is that enums nested in a class in C# cannot have the same name as a property of the class. My example:

public class Card
{
    public enum Suit
    {
        Clubs,
        Diamonds,
        Spades,
        Hearts
    }

    public enum Rank
    {
        Two,
        Three,
        ...
        King,
        Ace
    }

    public Suit Suit { get; private set; }
    public Rank Rank { get; private set; }
    ...
}

There are a few options to hack around this, but they don't seem right to me.

I could move the enums outside the class, but then you would just say Suit instead of Card.Suit, which seems wrong to me. What is a Suit outside the context of a Card?

I could move them outside the class and change them to something like CardSuit and CardRank, but then I'd feel like I'm baking context information into the name of the enum, when that should be handled by a class or namespace name.

I could change the names of the enums to Suits and Ranks, but this violates Microsoft's naming guidelines. And it just doesn't feel right.

I could change the property names. But to what? It feels intuitively right to me to want to say Suit = Card.Suit.Spades.

I could move the enums into a separate static class called CardInfo containing only these enums. If I can't come up with anything else, I think this is the best option.

So I'm wondering what other people have done in similar situations. It would also be nice to know why this is disallowed. Maybe Eric Lippert or someone could chime in on the decision to forbid it? It seems like it only creates ambiguity within the class, and this could be resolved by forcing the use of this.Suit for the property name. (Similar to disambiguating between locals and members.) I assume this was left out due to the "every feature starts with -100 points" thing, but I would be curious about discussions around this.

解决方案

It would also be nice to know why this is disallowed. Maybe Eric Lippert or someone could chime in on the decision to forbid it?

The point of the rule is to ensure that there is no ambiguity within the class when looking up a name. Certain regions of code are designated as defining a 'declaration space'. The fundamental rule of declaration spaces is no two things declared in the same declaration space have the same name (except for methods, which must differ by signature, not name.)

Making exceptions to this rule just makes things more confusing, not less confusing. I agree that it is vexing that you cannot have a property and an enum of the same name declared in the same declaration space, but once you start making exceptions then it just gets to be a mess. It's usually a nice property that a name uniquely identifies a method group, type parameter, property, and so on.

Note that this rule applies to things declared in a declaration space, not things used in a declaration space. It is perfectly legal to say "public Suit Suit { get; set; }" provided that the type Suit is not declared in the same declaration space as the property. When someone says "Suit.X", figuring out whether X is on the type (that is, X is a static member) or the property (that is, X is an instance member) is a bit tricky. See my article on how we do that for details:

http://blogs.msdn.com/ericlippert/archive/2009/07/06/color-color.aspx

这篇关于嵌套枚举和属性命名冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆