枚举是单数还是复数?

发布于 2024-08-02 19:11:10 字数 327 浏览 2 评论 0原文

枚举时使用单数还是复数?我认为在声明中使用复数是最有意义的

enum Weekdays
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

......但我认为在使用类型时使用单数更有意义,例如

Weekday firstDayOfWeek = Weekday.Monday;

我在某处读到建议使用单数whith常规枚举和带有标志的复数,但我想听听更多的优点和缺点。

Do you use singular or plural for enumerations? I think it makes best sense with plural in the declaration

enum Weekdays
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

... but I think it makes more sense with singular when using the type, e.g.

Weekday firstDayOfWeek = Weekday.Monday;

I read a recommendation somewhere to use singular whith regular enums and plural with flags, but I would like to hear some more pros and cons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

逆夏时光 2024-08-09 19:11:10

这是直接来自 Microsoft 的:

http://msdn.microsoft .com/en-us/library/4x252001(VS.71).aspx

对大多数枚举使用单一名称
类型,但对 Enum 使用复数名称
位字段类型。

Here it is straight from Microsoft:

http://msdn.microsoft.com/en-us/library/4x252001(VS.71).aspx

Use a singular name for most Enum
types, but use a plural name for Enum
types that are bit fields.

倾其所爱 2024-08-09 19:11:10

其中一项建议来自 .NET 框架设计指南,第 59-60 页:

不要对枚举使用单数类型名称,除非其值是位
字段。

公共枚举ConsoleColor {
  黑色的,
  蓝色的,
  青色,
  ...

对以位字段作为值的枚举使用复数类型名称,也称为标志枚举。

<前><代码>[标志]
公共枚举 ConsoleModifiers {
阿尔特,
控制,
转移
}

One recommendation comes from the .NET Framework Design Guidelines, page 59-60:

Do use a singular type name for an enumeration, unless its values are bit
fields.

public enum ConsoleColor {
  Black,
  Blue,
  Cyan,
  ...

Do use a plural type name for an enumeration with bit fields as values, also called a flags enum.

[Flags]
public enum ConsoleModifiers {
  Alt,
  Control,
  Shift
}
倾城泪 2024-08-09 19:11:10

在 .NET Framework 中,大多数“普通”枚举(例如 DayOfWeek)具有单数名称,而标志枚举(例如 StringSplitOptionsBindingFlags)具有复数名称名称。这是有道理的,因为标志枚举的值可以表示多个项目,但对于非标志枚举,它只能表示单个项目。

In the .NET Framework, most "normal" enums (e.g. DayOfWeek) have singular names and flag enums (e.g. StringSplitOptions, BindingFlags) have plural names. It makes sense, since a value of a flag enum can represent multiple items but for a non-flag enum, it can only represent a single item.

凝望流年 2024-08-09 19:11:10

一般来说,我认为枚举定义是类型定义,其值是
枚举是该类型可以具有的不同值;因此它有一个单一的名字:
枚举工作日 { SUNDAY、MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY、SATURDAY };
enum CoffeeSize { SMALL, MEDIUM, LARGE };

是的。如果您有将枚举实现为类的心理体验,那么
事实上,您为该类型使用单一名称应该表明它
对此类枚举使用单数名称是有意义的。例如,

struct Weekday {};

const Weekday SUNDAY;
const Weekday MONDAY;
const Weekday TUESDAY;

...

void func (Weekday *day)
{
   if (day == &SUNDAY)
       ...
}

对于那些喜欢枚举中的复数的人,您会将其命名为 struct Weekdays 吗?

In general, I consider an enum definition to be a type definition, with the values of the
enum being the different values the type can have; therefore it gets a singular name:
enum Weekday { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
enum CoffeeSize { SMALL, MEDIUM, LARGE };

Yes. If you do the mental experience of implementing the enums as classes, then
the fact that you'd use a singular name for the type should reveal that it
makes sense to use singular names for such enums . E.g.,

struct Weekday {};

const Weekday SUNDAY;
const Weekday MONDAY;
const Weekday TUESDAY;

...

void func (Weekday *day)
{
   if (day == &SUNDAY)
       ...
}

For who prefers plurals in enums, would you name that struct Weekdays?

一瞬间的火花 2024-08-09 19:11:10

一般来说,我认为枚举定义是类型定义,枚举的值是该类型可以具有的不同值;因此它有一个单一的名字:

enum Weekday { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };

enum CoffeeSize { SMALL, MEDIUM, LARGE };

In general, I consider an enum definition to be a type definition, with the values of the enum being the different values the type can have; therefore it gets a singular name:

enum Weekday { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };

enum CoffeeSize { SMALL, MEDIUM, LARGE };
拥抱我好吗 2024-08-09 19:11:10

Microsoft 建议对枚举使用单数名称,除非使用 Flags 属性。正如《框架设计指南》一书中所述,您不应在枚举类型名称后添加 Enum、Flags 等后缀,也不应在枚举值前添加缩写或首字母缩略词作为前缀,这在 VB 枚举中很常见。

Microsoft recommends using a singular name for enumerations unless it uses the Flags attribute. And as taken from the Framework Design Guidelines book you should not suffix enumeration type names with Enum, Flags, etc. and you should not prefix enumeration values with an abbreviation or acronym as was common with VB enumerations back in the day.

抚笙 2024-08-09 19:11:10

这是主观的,只要你保持一致,你使用什么并不重要(我个人使用单数,因为它是我的 Sql 约定的延续)

It's subjective and doesn't matter what you use, as long as you're consistent (personally I use singular as its a carry over from my Sql conventions)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文