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

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

问题描述

有一些相关问题此处这里,但他们并没有真正给我满意的答案。问题是嵌套在C#中的类中的枚举不能与该类的属性具有相同的名称。我的例子:

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.

我可以将课程以外的枚举移动,但是您只需说 Suit 而不是 Card.Suit ,这对我来说似乎是错误的。 c c c c

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?

我可以将他们移到课外,并将其更改为 CardSuit CardRank ,但是我觉得我正在将上下文信息打入枚举的名称,当时应该由类或命名空间名称来处理。

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.

我可以将枚举的名称更改为 Suits Ranks ,但这违反了微软的命名规则。而且只是感觉不到。

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.

我可以更改属性名称。但到什么?感觉直觉对我想说 Suit = Card.Suit.Spades

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

我可以移动枚举成一个单独的静态类,名为 CardInfo ,只包含这些枚举。如果我不能想出任何其他事情,我认为这是最好的选择。

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.

所以我想知道其他人在类似的情况下做了什么。这也是很高兴知道为什么这是不允许的。也许Eric Lippert或有人可以在决定禁止它吗?看起来它只是在类中产生歧义,这可以通过强制使用 this.Suit 来解决这个属性名。 (类似于本地人和成员之间的歧义)我认为这是由于每个功能都是以-100点开始的东西,但是对于这个讨论,我会好奇。

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.

推荐答案


这也是很高兴知道为什么这是不允许的。也许Eric Lippert或有人可以在决定禁止它吗?

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.

请注意,此规则适用在声明空间中声明为的东西,而不是在声明空间中使用 。如果没有在与属性相同的声明空间中声明Suit,则说public suit suit {get; set;}是完全合法的。当有人说Suit.X时,确定X是否在类型上(即X是一个静态成员)或属性(即X是一个实例成员)有点棘手。有关详细信息,请参阅我的文章:

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天全站免登陆