我问这个问题,尽管读过类似的,但不完全是我想在c#命名约定enum和匹配属性

我发现我倾向于用复数来命名枚举,然后用单数来“使用”它们,例如:

public enum EntityTypes {
  Type1, Type2
}

public class SomeClass {
  /*
    some codes
  */

  public EntityTypes EntityType {get; set;}

}

当然,这是我的风格,但谁能发现这种惯例的潜在问题?不过,我对“地位”这个词确实有一个“丑陋”的命名:

public enum OrderStatuses {
  Pending, Fulfilled, Error, Blah, Blah
}

public class SomeClass {
  /*
    some codes
  */

  public OrderStatuses OrderStatus {get; set;}

}

额外的信息: 也许我的问题还不够清楚。在为我所定义的枚举类型的变量命名时,我经常需要认真思考。我知道最佳实践,但这无助于减轻我命名这些变量的工作。

我不能暴露我所有的枚举属性(说“状态”)为“MyStatus”。

我的问题:有人能发现上面描述的约定的潜在问题吗?这与最佳实践无关。

问题改述:

好吧,我想我应该这样问这个问题:有人能想出一个很好的通用方式命名枚举类型,这样在使用时,命名枚举“实例”将非常直接?


当前回答

来晚了一点……

你的问题和你提到的问题(我问的是;-)之间有一个重要的区别:

将枚举定义放在类外,这允许枚举和属性具有相同的名称:

public enum EntityType { 
  Type1, Type2 
} 

public class SomeClass { 
  public EntityType EntityType {get; set;} // This is legal

}

在这种情况下,我将遵循MS指南,为枚举使用单数名称(为标志使用复数名称)。这可能是最简单的解决方案。

我的问题(在另一个问题中)是当枚举定义在类的范围内时,阻止使用恰好以枚举命名的属性。

其他回答

在另一个线程中,枚举和匹配属性的c#命名约定有人指出了我认为非常好的想法:

“我知道我的建议违背了。net命名惯例,但我个人认为枚举前缀为'E',枚举标志前缀为'F'(类似于我们为接口前缀为'I')。”

这是少数几个我不同意公约的地方之一,足以反对它。TBH,我讨厌枚举的定义和它的实例可以有相同的名字。我把所有的枚举都加上了“Enum”,因为它清楚地表明了它在任何给定用法中的上下文。在我看来,这让代码可读性更强。

public enum PersonTypesEnum {
    smart,
    sad,
    funny,
    angry
}


public class Person {   
    public PersonTypesEnum PersonType {get; set;}
}

没有人会混淆什么是enum,什么是它的实例。

The reason for using a plural for a enum declaration is the fact that ( at the time of declaration ) we declare it with multiple values, so plural seems good... But we ignore the fact that enum when declared specifies what value it can have ( from the given set of values ). It doesn't mean that the instance of that enum will store multiple values..... When we write: enum Days { MON, TUE, WED, THU, FRI, SAT, SUN}; We are making it plural because of the multiple values provide.. However when used (Days day = Days.MON; ) we completely ignore that instance of that enum is supposed to have a single value.... So when we write : enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }; We mean that there is a enum that can have any one day as its value, so singular is more appropriate. Although (already described above ), to get around this without using singular names is by using any kind of Indicator, like DayEnum or EDay ( i prefer the second one)....

微软建议使用单数Enum,除非Enum表示位字段(也可以使用FlagsAttribute)。参见枚举类型命名约定(微软命名指南的一个子集)。

就你的澄清,我认为以下任何一条都没有问题:

public enum OrderStatus { Pending, Fulfilled, Error };

public class SomeClass { 
    public OrderStatus OrderStatus { get; set; }
}

or

public enum OrderStatus { Pending, Fulfilled, Error };

public class SomeClass {
    public OrderStatus Status { get; set; }
}

一般来说,最佳实践建议是单数,除了那些附加了[Flags]属性的枚举(因此可以包含位字段),它应该是复数。

在阅读了你编辑的问题后,我有一种感觉,你可能认为属性名或变量名必须与枚举类型名不同……它不是。下面的内容完全没问题……

  public enum Status { New, Edited, Approved, Cancelled, Closed }

  public class Order
  {
      private Status stat;
      public Status Status
      { 
         get { return stat; }
         set { stat = value; }
      }
  }