枚举和匹配属性的 C# 命名约定
我经常发现自己实现了一个类,将某种自己的状态属性维护为枚举:我有一个 Status 枚举和一个 Status 类型的 Status 属性。 我应该如何解决这个名称冲突?
public class Car
{
public enum Status
{
Off,
Starting,
Moving
};
Status status = Status.Off;
public Status Status // <===== Won't compile =====
{
get { return status; }
set { status = value; DoSomething(); }
}
}
如果 Status 枚举对于不同类型是通用的,我会将其放在类之外,问题就可以解决。 但 Status 仅适用于 Car,因此在类之外声明枚举是没有意义的。
在这种情况下您使用什么命名约定?
注意:这个问题在这个问题的答案的评论中进行了部分辩论。 由于这不是主要问题,因此没有得到太多关注。
编辑:Filip Ekberg 针对“状态”的具体情况提出了一种 IMO 出色的解决方法。 不过,我很想了解枚举/属性名称不同的解决方案,如 Michael Prewecki 的答案。
EDIT2(2010 年 5 月):我最喜欢的解决方案是按照 Chris S 的建议将枚举类型名称复数。根据 MS 指南,这应该仅用于标志枚举。 但我越来越喜欢它了。 我现在也将它用于常规枚举。
I often find myself implementing a class maintaining some kind of own status property as an enum: I have a Status enum and ONE Status property of Status type. How should I solve this name conflict?
public class Car
{
public enum Status
{
Off,
Starting,
Moving
};
Status status = Status.Off;
public Status Status // <===== Won't compile =====
{
get { return status; }
set { status = value; DoSomething(); }
}
}
If the Status enum were common to different types, I'd put it outside the class and the problem would be solved. But Status applies to Car only hence it doesn't make sense to declare the enum outside the class.
What naming convention do you use in this case?
NB: This question was partially debated in comments of an answer of this question. Since it wasn't the main question, it didn't get much visibility.
EDIT: Filip Ekberg suggests an IMO excellent workaround for the specific case of 'Status'. Yet I'd be interesting to read about solutions where the name of the enum/property is different, as in Michael Prewecki's answer.
EDIT2 (May 2010): My favorite solution is to pluralize the enum type name, as suggested by Chris S. According to MS guidelines, this should be used for flag enums only. But I've come to like it more and more. I now use it for regular enums as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
“关闭”、“开始”和“移动”的定义就是我所说的“状态”。 当您暗示您正在使用“状态”时,它就是您的“状态”。 所以!
如果我们再举一个例子,在这种情况下您想使用“类型”一词:
您确实需要看到枚举和枚举之间存在差异,对吧? 但是,在创建框架或谈论架构时,您需要关注相似之处,好吧,让我们找到它们:
当某物设置为“状态”时,它被定义为“事物”状态
示例:汽车的状态处于运行状态、停止状态等。
在第二个示例中您想要实现的目标在某种程度上是这样的:
您可能认为这与我一直向其他人宣讲的内容相反,您需要遵循一个标准。 但是,你遵循一个标准! Sql 案例也是一个特定的案例,因此需要一些特定的解决方案。
但是,枚举可以在您的 System.Data 空间中重复使用,这就是模式的全部内容。
“类型”的另一个例子是“动物”,其中类型定义了物种。
这是遵循一种模式,您不需要专门为此“了解”对象,并且您没有指定“AnimalType”或“DataReaderType”,您可以在您选择的命名空间中重复使用枚举。
The definition of "Off", "Starting" and "Moving" is what i would call a "State". And when you are implying that you are using a "State", it is your "Status". So!
If we take another example from the one stated where you'd like to use the word "Type" such in this case:
You really need to see that there is a difference between enums and enums, right? But when creating a framework or talking about architecture you need to focus on the simillarities, ok lets find them:
When something is set to a State, it's defined as the "things" Status
Example: The Car's Status is in Running State, Stopped State, and so on.
What you want to acheive in the second example is somewhat this:
You might think that this says against what i've been preaching about to others, that you need to follow a standard. But, you are following a standard! The Sql-case is a specific case aswell and therefore need a somewhat specific solution.
However, the enum would be re-usable within your
System.Data
space, and that's what the patterns is all about.Another case to look at with the "Type" is "Animal" where Type defines the Species.
This is following a pattern, you don't specificly need to "know" the Object for this and you are not specifing "AnimalType" or "DataReaderType", you can re-use the enums in your namespace of choice.
我将在讨论中添加我的 1 欧元,但它可能不会添加任何新内容。
显而易见的解决方案是将 Status 移出嵌套枚举。 大多数 .NET 枚举(可能除了 Windows.Forms 命名空间中的一些枚举)都不是嵌套的,这使得使用 API 的开发人员使用起来很烦人,因为必须在类名前添加前缀。
没有提到的一件事是,根据 MSDN 指南,标志枚举应该 复数名词,您可能已经知道(Status 是一个简单的枚举,因此应使用单数名词)。
State(枚举称为States)是呼格,“Status”是一个名词的主格,英语像我们大多数语言一样从拉丁语吸收而来。 呼格是根据名词的条件而命名的,主格是动词的主语。
换句话说,当汽车正在移动时,这就是动词 - 移动是它的状态。 但汽车并没有熄火,而是发动机熄火了。 它也不会启动,而是引擎启动(您可能在这里选择了一个示例,因此这可能无关紧要)。
状态是一个如此广义的名词,描述它所指的状态不是更好吗? 就像我上面所做的那样
,在我看来,类型示例也不是指阅读器类型,而是指其数据库。 如果您描述的读者的数据库产品不一定与读者的类型相关(例如,读者的类型可能是仅前向的、缓存的等等),我会更喜欢它。 因此,
实际上这种情况永远不会发生,因为它们是作为驱动程序和继承链实现的,而不是使用枚举,这就是上面的行看起来不自然的原因。
I'll add my 1 euro to the discussion but it's probably not adding anything new.
The obvious solution is to move Status out of being a nested Enum. Most .NET enums (except possibly some in Windows.Forms namespace) aren't nested and it makes it annoying to use for the developer consuming your API, having to prefix the classname.
One thing that hasn't been mentioned is that flag enums according to MSDN guidelines should be pluralized nouns which you probably already know (Status is a simple enum so singular nouns should be used).
State (enum called States) is the vocative, "Status" is the nominative of a noun that the English like most of our language absorbed from Latin. Vocative is what you name a noun for its condition and nominative is the subject of the verb.
So in other words when the car is moving, that's the verb - moving is its status. But the car doesn't go off, its engine does. Nor does it start, the engine does (you probably picked an example here so this might be irrelevant).
State is such a generalised noun wouldn't it be better to describe what state it is referring to? Like I did above
The type example also in my view doesn't refer to the reader type, but its database. I would prefer it if you were describing the reader's database product which isn't necessarily relevant to the type of reader (e.g. the type of reader might be forward only, cached and so on). So
In reality this never happens as they're implemented as drivers and an inheritance chain instead of using enums which is why the line above doesn't look natural.
我认为这里真正的问题是枚举 Status 封装在您的类中,这样
Car.Status
对于属性Status
和枚举Status 来说都是不明确的更好
的是,将枚举放在类之外:
更新
由于下面的评论,我将解释上面的设计。
我不相信枚举或类或任何其他对象应该驻留在另一个类中,除非它在该类中是完全私有的。 以上面的例子为例:
虽然一些评论者认为 Status 在类 Car 的范围之外没有任何含义,但您设置 public 属性的事实意味着还有其他含义程序中将使用该枚举的部分:
对我来说是一种代码味道。 如果我要查看
Car
外部的状态,我也可以将其定义在外部。因此,我可能会将其重命名为:
但是,如果该枚举将在并且仅在汽车类中使用,那么我可以在那里声明该枚举。
I think the real problem here is that the enum Status is encapsulated within your class, such that
Car.Status
is ambiguous to both the propertyStatus
and the enumStatus
Better yet, put your enum outside of the class:
UPDATE
Due to the comments below, I'll explain my design above.
I'm one who doesn't believe that enums or classes or any other object should reside inside another class, unless it will be totally private within that class. Take the above example, for instance:
While some commenters would argue that Status doesn't have any meaning outside the scope of the class Car, the fact that you are setting a public property means that there are other parts of the program that will use that enum:
And that to me is a code smell. If I'm going to look at that status outside of
Car
, I might as well define it outside as well.As such, I will probably just rename it as:
However, if that enum will be used within and only within the car class, then I'm fine with declaring the enum there.
那些讨厌匈牙利记数法及其变体的人都该死。 我使用枚举后缀的约定 - 等待它 -
Enum
。 因此,我从来没有遇到过你所描述的问题,浪费时间担心如何称呼它们,并且代码是可读的并且是自描述的。Haters of Hungarian notation and its variants be damned. I use a convention of suffixing enums with - wait for it -
Enum
. I consequently never have the problem you describe, waste time worrying about what to call them and the code is readable and self-descriptive to boot.我知道我的建议违反了 .NET 命名约定,但我个人在枚举上添加了“E”前缀,在枚举标志上添加了“F”(类似于我们在接口上添加了“I”前缀)。 我真的不明白为什么这不是惯例。 枚举/标志是一种特殊情况,例如永远不会改变其类型的接口。 它不仅清楚地表明它是什么,而且很容易输入智能感知,因为前缀将过滤大多数其他类型/变量/等,并且您不会遇到这些命名冲突。
这也将解决另一个问题,例如在 WPF 中,它们使用静态类,如枚举(例如 FontWeights),这些类具有预定义的类型实例,但如果不搜索它,您将不知道。 如果他们只是用“E”作为前缀,那么您所要做的就是输入字符来查找这些特殊的静态类。
I know my suggestion goes against the .NET Naming conventions, but I personally prefix enums with 'E' and enum flags with 'F' (similar to how we prefix Interfaces with 'I'). I really do not understand why this is not the convention. Enums/Flags are a special case like Interfaces that will never change their type. Not only does it make it clear what it is, it's very easy to type in intellisense since the prefix will filter most other types/variables/etc, and you won't have these naming clashes.
And that would also solve another problem where for examples in WPF they use static classes like enums (e.g. FontWeights) that have pre-defined instances of types but you would not know if you don't search for it. If they just prefixed them with 'E', all you would have to do is type on character to find these special static classes.
我会将属性的名称更改为“CurrentStatus”之类的名称。 快速又简单:)
I'd change the name of the property to something like "CurrentStatus". Quick an easy :)
我建议在类型名称中添加“Option”(如果包含位标志,则添加“Flag”),即类型为 Car.StatusOption,属性为 Car.Status。
与复数相比,这可以避免在创建枚举类型的集合时发生命名冲突,您通常希望对集合属性进行复数,而不是枚举类型。
I suggest adding "Option" to the type name (or Flag if it contains bit flags), i.e. type is Car.StatusOption and property is Car.Status.
Compared to pluralizing, this avoids naming collisions when creating collections of the enum type, where you would normally want to pluralize the collection property, not the enum type.
我通常为枚举添加前缀,例如 CarStatus。 我想这完全取决于您正在合作的团队(如果他们有任何此类事情的规则/流程)和对象的使用情况。 只是我的 2 美分(:
I usually prefix the enums, e.g. CarStatus. I suppose it all depends on team you're working with (if they have any rules/ processes for that sort of thing) and the objects usage. Just my 2 cents (: