C# 枚举和属性命名冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/211567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enum and property naming conflicts
提问by AndersF
When using a class that has an enum property, one usually gets a naming conflict between the property name and the enum type. Example:
当使用具有枚举属性的类时,通常会在属性名称和枚举类型之间发生命名冲突。例子:
enum Day{ Monday, Tuesday, ... }
class MyDateClass
{
private Day day;
public Day Day{ get{ return day; } }
}
Since only flags enums should have plural names, naming the enum "Days" is not the way to go for a non-flag enum. In the above example you could use some variation like "WeekDay" for either the enum or the property. But in the general case there are no good variations like that so you end up using properties like "FooMode" or "BarKind" for an object with enum properties of Foo and Bar type. Not so elegant.
由于只有标志枚举应该有复数名称,因此将枚举命名为“Days”并不是非标志枚举的方法。在上面的示例中,您可以对枚举或属性使用一些变体,例如“WeekDay”。但在一般情况下,没有像这样好的变体,因此您最终会使用诸如“FooMode”或“BarKind”之类的属性来处理具有 Foo 和 Bar 类型的枚举属性的对象。没那么优雅。
How do you usually name enums and properties in this scenario?
在这种情况下,您通常如何命名枚举和属性?
Thanks for the quick responses. Another question: why is it not recommended to nest public enums, and how do you resolve the naming issues if you want to nest public enums?
感谢您的快速回复。另一个问题:为什么不建议嵌套公共枚举,如果要嵌套公共枚举,如何解决命名问题?
class Vehicle
{
enum Kind{ Car, Bike }
public Kind Kind{ get{ return ... } }
}
class Meal
{
enum Kind{ Dessert, MainCourse }
public Kind Kind{ get{ return ... } }
}
In the scenario above, given that Meal and Vehicle share the same namespace, I can't move "Kind" outside either of the classes without renaming it MealKind and VehicleKind respectively. I like the look of
在上面的场景中,考虑到 Meal 和 Vehicle 共享相同的命名空间,我不能将“Kind”移到任何一个类之外,而不分别将其重命名为 MealKind 和 VehicleKind。我喜欢的样子
myVehicle.Kind = Vehicle.Kind.Car
But that is not what the guidlines recommend. What would be the best practice here? Never to use nested public enums and instead name them VehicleKind etc.?
但这不是指南推荐的内容。这里的最佳做法是什么?永远不要使用嵌套的公共枚举,而是将它们命名为 VehicleKind 等?
回答by Jon Skeet
So long as the enumeration isn't nested within MyDateClass, I don't see that that's a problem. It's far from uncommon (in my experience) to have a property with the same name as the type it returns. I'll see if I can find some examples in the framework...
只要枚举不嵌套在 MyDateClass 中,我就不认为这是一个问题。拥有与它返回的类型同名的属性远非罕见(以我的经验)。我会看看是否可以在框架中找到一些示例...
EDIT: First example: DateTimeOffset.DateTime (not an enum, but that's somewhat irrelevant)
编辑:第一个示例:DateTimeOffset.DateTime(不是枚举,但这有点无关紧要)
回答by OregonGhost
There is no conflict. In fact, the .NET Framework style guide encourages you to do this, e.g. if you have a class that has a single property of a type (no matter if enum or class), then you should name it the same. Typical example is a Color property of type Color. It's fine, unless there are two colors - in that case both should add something to the name (i.e. BackColor and ForeColor, instead of Color and BackColor).
没有冲突。事实上,.NET Framework 样式指南鼓励您这样做,例如,如果您有一个具有单一类型属性的类(无论是枚举还是类),那么您应该将其命名为相同的。典型示例是 Color 类型的 Color 属性。没关系,除非有两种颜色 - 在这种情况下,两者都应该在名称中添加一些东西(即 BackColor 和 ForeColor,而不是 Color 和 BackColor)。