C# 枚举命名约定 - 复数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1405851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enum Naming Convention - Plural
提问by o.k.w
I'm asking this question despite having read similar but not exactly what I want at C# naming convention for enum and matching property
尽管在枚举和匹配属性的 C# 命名约定中阅读了类似但不完全是我想要的内容,但我仍在问这个问题
I found I have a tendency to name enums in plural and then 'use' them as singular, example:
我发现我倾向于以复数形式命名枚举,然后将它们“使用”为单数,例如:
public enum EntityTypes {
Type1, Type2
}
public class SomeClass {
/*
some codes
*/
public EntityTypes EntityType {get; set;}
}
Of course it works and this is my style, but can anyone find potential problem with such convention? I do have an "ugly" naming with the word "Status" though:
当然它有效,这是我的风格,但是任何人都可以找到这种约定的潜在问题吗?不过,我确实有一个带有“状态”一词的“丑陋”命名:
public enum OrderStatuses {
Pending, Fulfilled, Error, Blah, Blah
}
public class SomeClass {
/*
some codes
*/
public OrderStatuses OrderStatus {get; set;}
}
Additional Info: Maybe my question wasn't clear enough. I often have to think hard when naming the variables of the my defined enum types. I know the best practice, but it doesn't help to ease my job of naming those variables.
附加信息:也许我的问题不够清楚。在命名我定义的枚举类型的变量时,我经常不得不仔细考虑。我知道最佳实践,但它无助于减轻我命名这些变量的工作。
I can't possibly expose all my enum properties (say "Status") as "MyStatus".
我不可能将我所有的枚举属性(比如“Status”)公开为“MyStatus”。
My question: Can anyone find potential problem with my convention described above? It is NOT about best practice.
我的问题:任何人都可以找到上述约定的潜在问题吗?这与最佳实践无关。
Question rephrase:
问题改写:
Well, I guess I should ask the question this way: Can someone come out a good generic way of naming the enum type such that when used, the naming of the enum 'instance' will be pretty straightforward?
好吧,我想我应该这样问这个问题:有人可以提出一种很好的通用方法来命名枚举类型,这样在使用时,枚举“实例”的命名将非常简单?
采纳答案by jason
Microsoft recommends using singular for Enum
s unless the Enum
represents bit fields (use the FlagsAttribute
as well). See Enumeration Type Naming Conventions(a subset of Microsoft's Naming Guidelines).
Microsoft 建议对Enum
s使用单数,除非Enum
表示位字段(也使用 the FlagsAttribute
)。请参阅枚举类型命名约定(Microsoft命名指南的子集)。
To respond to your clarification, I see nothing wrong with either of the following:
为了回应您的澄清,我认为以下任何一项都没有问题:
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; }
}
回答by Charles Bretana
In general, the best practice recommendation is singular, except for those enums that have the [Flags] attribute attached to them, (and which therefore can contain bit fields), which should be plural.
一般来说,最佳实践建议是单数,除了那些附加了 [Flags] 属性的枚举(因此可以包含位字段),它们应该是复数。
After reading your edited question, I get the feeling you may think the property name or variable name has to be different from the enum type name... It doesn't. The following is perfectly fine...
阅读您编辑的问题后,我感觉您可能认为属性名称或变量名称必须与枚举类型名称不同......事实并非如此。以下完全没问题...
public enum Status { New, Edited, Approved, Cancelled, Closed }
public class Order
{
private Status stat;
public Status Status
{
get { return stat; }
set { stat = value; }
}
}
回答by Bob Kaufman
I started out naming enums in the plural but have since changed to singular. Just seems to make more sense in the context of where they're used.
我开始以复数命名枚举,但后来改为单数。在使用它们的上下文中似乎更有意义。
enum Status { Unknown = 0, Incomplete, Ready }
Status myStatus = Status.Ready;
Compare to:
相比于:
Statuses myStatus = Statuses.Ready;
I find the singular form to sound more natural in context. We are in agreement that when declaring the enum, which happens in one place, we're thinking "this is a group of whatevers", but when using it, presumably in many places, that we're thinking "this is one whatever".
我发现单数形式在上下文中听起来更自然。我们一致认为,在声明发生在一个地方的枚举时,我们在想“这是一组任何事物”,但是在使用它时,大概在许多地方,我们正在考虑“这是一个事物” .
回答by Jeremy Cron
Best Practice - use singular. You have a list of items that make up an Enum. Using an item in the list sounds strange when you say Versions.1_0
. It makes more sense to say Version.1_0
since there is only one 1_0 Version.
最佳实践 - 使用单数。您有一个组成 Enum 的项目列表。当您说 时,使用列表中的项目听起来很奇怪Versions.1_0
。Version.1_0
因为只有一个 1_0 版本,所以说更有意义。
回答by Kyle Rozendo
The situation never really applies to plural.
这种情况从未真正适用于复数。
An enum
shows an attribute of something or another. I'll give an example:
Anenum
显示某物的属性。我举个例子:
enum Humour
{
Irony,
Sarcasm,
Slapstick,
Nothing
}
You can have one type, but try think of it in the multiple, rather than plural:
你可以有一种类型,但试着用复数而不是复数来考虑它:
Humour.Irony | Humour.Sarcasm
Humour.Irony | Humour.Sarcasm
Rather than
而不是
Humours { Irony, Sarcasm }
Humours { Irony, Sarcasm }
You have a sense of humour, you don't have a sense of humours.
你有幽默感,你没有幽默感。
回答by RenniePet
On the other thread C# naming convention for enum and matching propertysomeone pointed out what I think is a very good idea:
在另一个线程C# 枚举和匹配属性的命名约定上,有人指出我认为是一个非常好的主意:
"I know my suggestion goes against the .NET Naming conventions, but I personally prefix enums with 'E' and enum flags with 'F' (similar to how we prefix Interfaces with 'I')."
“我知道我的建议违反了 .NET 命名约定,但我个人在枚举前加上 'E',在枚举标志前加上 'F'(类似于我们在接口前加上 'I' 的方式)。”
回答by Serge Wautier
Coming in a bit late...
来的有点晚...
There's an important difference between your question and the one you mention(which I asked ;-):
您的问题与您提到的问题(我问;-)之间存在重要区别:
You put the enum definition out of the class, which allows you to have the same name for the enum and the property:
您将枚举定义放在类之外,这允许您为枚举和属性使用相同的名称:
public enum EntityType {
Type1, Type2
}
public class SomeClass {
public EntityType EntityType {get; set;} // This is legal
}
In this case, I'd follow the MS guidelins and use a singular name for the enum (plural for flags). It's probaby the easiest solution.
在这种情况下,我会遵循 MS 指南并为枚举使用单数名称(标志为复数)。这可能是最简单的解决方案。
My problem (in the other question) is when the enum is defined in the scope of the class, preventing the use of a property named exactly after the enum.
我的问题(在另一个问题中)是在类的范围内定义枚举时,阻止使用完全以枚举命名的属性。
回答by MiloNC
If you are trying to write straightforward, yet forbidden code like this:
如果您尝试编写简单但被禁止的代码,如下所示:
public class Person
{
public enum Gender
{
Male,
Female
}
//Won't compile: auto-property has same name as enum
public Gender Gender { get; set; }
}
Your options are:
您的选择是:
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; } }
忽略 MS 建议并在枚举名称上使用前缀或后缀:
public class Person { public enum GenderEnum { Male, Female } public GenderEnum Gender { get; set; } }
将枚举定义移到类之外,最好是移到另一个类中。对于上述问题,这里有一个简单的解决方案:
public class Characteristics { public enum Gender { Male, Female } } public class Person { public Characteristics.Gender Gender { get; set; } }
回答by Heather
This is one of the few places that I disagree with the convention enough to go against it. TBH, I HATE that the definition of an enum and the instance of it can have the same name. I postfix all of my Enums with "Enum" specifically because it makes it clear what the context of it is in any given usage. IMO it makes the code much more readable.
这是我不同意公约足以反对它的少数几个地方之一。TBH,我讨厌枚举的定义和它的实例可以具有相同的名称。我专门用“Enum”后缀我的所有枚举,因为它清楚地表明在任何给定用法中它的上下文是什么。IMO 它使代码更具可读性。
public enum PersonTypesEnum {
smart,
sad,
funny,
angry
}
public class Person {
public PersonTypesEnum PersonType {get; set;}
}
Nobody will ever confuse what is the enum and what is the instance of it.
没有人会混淆什么是枚举和什么是它的实例。