我问这个问题,尽管读过类似的,但不完全是我想在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; }
}
如果你想写一些直截了当的,但被禁止的代码,像这样:
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; }
}