对于枚举和匹配性C#命名规则 [英] C# naming convention for enum and matching property

查看:923
本文介绍了对于枚举和匹配性C#命名规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己实现一个类保持某种自己的状态属性为一个枚举:我有一个状态枚举和状态类的一个状态性。我应该如何解决这个名称冲突?

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.

编辑:菲利普·埃克伯格建议一个海事组织优秀的解决方法状态的具体情况。然而,我会很有趣的阅读解决方案,其中枚举/属性的名称是不同的,如迈克尔prewecki的的的答案的。

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(2010年5月):我最喜欢的解决办法是复数枚举类型的名称,所建议的克里斯S.根据MS的指导方针,这应该仅用于标志枚举。但我已经开始喜欢它越来越多。我现在用它定期枚举为好。

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.

推荐答案

我要加我1欧元,为的讨论,但它可能不会增加任何新的东西。

I'll add my 1 euro to the discussion but it's probably not adding anything new.

显而易见的解决方案是将状态出的是一个嵌套枚举。大多数.NET枚举(可能除了一些Windows.Forms的命名空间)没有嵌套,这使得它恼人用于开发消费你的API,不必preFIX类名。

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.

尚未提及一件事的是标志枚举根据MSDN准则应的将复数名词,你可能已经知道(状态是一个简单的枚举所以单数名词应使用)。

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

public class Car
{
  VehicleState _vehicleState= VehicleState.Stationary;

  public VehicleState VehicleState 
  {
    get { return _vehicleState; }
    set { _vehicleState = value; DoSomething(); }
  }
}

public enum VehicleState
{
    Stationary, Idle, Moving
}

国家就是这样一个广义的名词,岂不是更好地描述它所指的是什么状态?像我上面

State is such a generalised noun wouldn't it be better to describe what state it is referring to? Like I did above

还类型例如在我的视图不指读者类型,但它的数据库。我想preFER它,如果你描述了读者的数据库产品,并不一定是相关的阅读器的类型(如阅读器的类型可能是只向前,缓存等)。因此,

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

reader.Database = Databases.Oracle;

在现实中,这不会发生,因为它们可以作为驱动器以及一个继承链,而不是使用枚举这就是为什么上面的线看起来不自然的实现。

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.

这篇关于对于枚举和匹配性C#命名规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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