我问这个问题,尽管读过类似的,但不完全是我想在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 class Person
    {
        public enum Gender
        {
            Male,
            Female
        }
        //Won't compile: auto-property has same name as enum
        public Gender Gender { get; set; }  
    }

你的选择是:

Ignore the MS recommendation and use a prefix or suffix on the enum name: public class Person { public enum GenderEnum { Male, Female } public GenderEnum Gender { get; set; } } Move the enum definition outside the class, preferably into another class. Here is an easy solution to the above: public class Characteristics { public enum Gender { Male, Female } } public class Person { public Characteristics.Gender Gender { 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; }
      }
  }

这种情况从来不适用于复数。

枚举显示某物的属性。我举个例子:

enum Humour
{
  Irony,
  Sarcasm,
  Slapstick,
  Nothing
}

你可以有一种类型,但试着用复数来思考,而不是复数:

幽默。讽刺幽默。讽刺

而不是

幽默{讽刺,讽刺}

你有幽默感,你没有幽默感。

如果你想写一些直截了当的,但被禁止的代码,像这样:

    public class Person
    {
        public enum Gender
        {
            Male,
            Female
        }
        //Won't compile: auto-property has same name as enum
        public Gender Gender { get; set; }  
    }

你的选择是:

Ignore the MS recommendation and use a prefix or suffix on the enum name: public class Person { public enum GenderEnum { Male, Female } public GenderEnum Gender { get; set; } } Move the enum definition outside the class, preferably into another class. Here is an easy solution to the above: public class Characteristics { public enum Gender { Male, Female } } public class Person { public Characteristics.Gender Gender { get; set; } }

微软建议使用单数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; }
}

来晚了一点……

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

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

public enum EntityType { 
  Type1, Type2 
} 

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

}

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

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