枚举和财产命名冲突 [英] Enum and property naming conflicts

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

问题描述

当使用具有枚举属性类,人们往往会获得属性名称和枚举类型之间的命名冲突。例如:

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; } }
}

由于只有标​​志枚举应该具有多个名字,命名枚举天是不是要走的路了非标志的枚举。在上面的例子中,你可以使用类似工作日对于任何一个枚举或财产某些变化。但在一般情况下,所以你最终会使用像FooMode或BarKind属性的对象的Foo和Bar枚举类型的属性没有很好的变化之类的。事实并非如此优雅。

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.

你通常如何命名枚举和属性在这种情况?

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 ... } }
}

在上述情况下,考虑到膳食和汽车共享同一个命名空间,我不能动。好心肠之外任一类的没有分别重命名它MealKind和VehicleKind。我喜欢的样子

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

myVehicle.Kind = Vehicle.Kind.Car

但是,这并不是什么guidlines建议。什么是最好的做法吗?千万不要使用嵌套公共枚举,而是他们的名字VehicleKind等?

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.?

推荐答案

有没有冲突。事实上, .NET框架的风格指南鼓励你这样做,例如:如果你有,有一个类型的单一属性的类(无论枚举或类),那么你应该将其命名为相同的。典型的例子是Color类型的Color属性。这很好,除非有两种颜色 - 在这种情况下,两者都应该增加(而不是颜色与背景色,即背景色和前景色,)的东西,名字

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).

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

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