枚举和匹配属性的 C# 命名约定 [英] C# naming convention for enum and matching property

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

问题描述

我经常发现自己实现了一个类,该类维护某种自己的状态属性作为枚举:我有一个 Status 枚举和一个 Status 类型的 Status 属性.我应该如何解决这个名称冲突?

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

如果 Status 枚举对于不同类型是通用的,我会将它放在类之外,问题就会得到解决.但是 Status 仅适用于 Car,因此在类之外声明枚举是没有意义的.

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.

Filip Ekberg 针对状态"的特定情况提出了 IMO 出色的解决方法.然而,我会很有趣地阅读有关枚举/属性名称不同的解决方案,如 Michael Prewecki 的 answer.

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 月):我最喜欢的解决方案是按照 Chris 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.

显而易见的解决方案是将 Status 从嵌套的 Enum 中移出.大多数 .NET 枚举(可能除了 Windows.Forms 命名空间中的一些)不是嵌套的,这使得使用 API 的开发人员使用起来很烦人,必须在类名前加上前缀.

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 指南的标志枚举应该是您可能已经知道的复数名词(Status 是一个简单的枚举,因此应使用单数名词).

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(枚举称为States)是呼格,Status"是名词的主格,英语就像我们的大部分语言一样从拉丁语中吸收过来的.主格是您根据其条件命名名词的名称,而主格是动词的主语.

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 是一个如此笼统的名词,描述它所指的状态不是更好吗?就像我上面做的

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

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