对于具有前导数字的枚举值是否有命名约定?

发布于 2024-08-04 17:44:25 字数 348 浏览 3 评论 0 原文

我的 ac# 枚举如下所示:

public enum EthernetLinkSpeed {

    [Description("10BASE-T")]
    _10BaseT,

    [Description("100BASE-T")]
    _100BaseT,

    [Description("1000BASE-T")]
    _1000BaseT,

    [Description("Disconnected")]
    Disconnected
}

我为每个值添加了一个前导下划线以使编译器满意。

像这样的枚举有标准的命名约定吗?我使用的下划线似乎不是最好的选择。

I have a c# enumeration that looks like this:

public enum EthernetLinkSpeed {

    [Description("10BASE-T")]
    _10BaseT,

    [Description("100BASE-T")]
    _100BaseT,

    [Description("1000BASE-T")]
    _1000BaseT,

    [Description("Disconnected")]
    Disconnected
}

I added a leading underscore to each value to make the compiler happy.

Is there a standard naming convention for enumerations like this? The underscore I've used doesn't seem like the best choice.

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

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

发布评论

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

评论(7

_畞蕅 2024-08-11 17:44:25

我不知道有什么约定,但是怎么样

public enum EthernetLinkSpeed {
 Link10BaseT, Link1000BaseT, LinkDisconnected
}

I know of no convention, but how about

public enum EthernetLinkSpeed {
 Link10BaseT, Link1000BaseT, LinkDisconnected
}
走走停停 2024-08-11 17:44:25

我只是在这种情况下寻找更具描述性的东西。例如,既然你有一个“Disconnected”枚举值,我会使用类似的东西:

public enum EthernetLinkSpeed {
    Connected10BaseT,
    Connected100BaseT,
    Connected1000BaseT,
    Disconnected
}

因为这些只是编译时的,所以只要你愿意,让它们长一点也没有什么坏处,即使让它们长只是意味着让它们具有足够的描述性通过编译器的命名规则。

I just look for something more descriptive in this case. For instance, since you have a "Disconnected" enum value, I would use something like:

public enum EthernetLinkSpeed {
    Connected10BaseT,
    Connected100BaseT,
    Connected1000BaseT,
    Disconnected
}

Since these are compile-time only, there's no damage in having them as long you like, even if making them long just means making them descriptive enough to pass the compiler's naming rules.

拔了角的鹿 2024-08-11 17:44:25

不是直接答案,而是内置的 NetworkInterfaceType 枚举包括以下值:

以太网、Ethernet3Megabit、FastEthernetT、FastEthernetFx、千兆以太网

这对我来说有点难看,但我可能会考虑为您的枚举使用 Ethernet 前缀:

public enum EthernetLinkSpeed
{
    [Description("10BASE-T")]
    Ethernet10BaseT,

    [Description("100BASE-T")]
    Ethernet100BaseT,

    [Description("1000BASE-T")]
    Ethernet1000BaseT,

    [Description("Disconnected")]
    Disconnected
}

Not a direct answer, but the built-in NetworkInterfaceType enum includes the following values:

Ethernet, Ethernet3Megabit, FastEthernetT, FastEthernetFx, GigabitEthernet

It's a bit ugly for my liking, but I might consider using an Ethernet prefix for your enum:

public enum EthernetLinkSpeed
{
    [Description("10BASE-T")]
    Ethernet10BaseT,

    [Description("100BASE-T")]
    Ethernet100BaseT,

    [Description("1000BASE-T")]
    Ethernet1000BaseT,

    [Description("Disconnected")]
    Disconnected
}
琉璃繁缕 2024-08-11 17:44:25

我不确定是否有约定,但主观上这样的事情可能不那么“丑陋”:

public enum EthernetLinkSpeed {

    [Description("10BASE-T")]
    TenBaseT,

    [Description("100BASE-T")]
    OneHundredBaseT,

    [Description("1000BASE-T")]
    OneThousandBaseT,

    [Description("Disconnected")]
    Disconnected
}

I'm not sure if there is a convention, but something like this may be subjectively less 'ugly':

public enum EthernetLinkSpeed {

    [Description("10BASE-T")]
    TenBaseT,

    [Description("100BASE-T")]
    OneHundredBaseT,

    [Description("1000BASE-T")]
    OneThousandBaseT,

    [Description("Disconnected")]
    Disconnected
}
少跟Wǒ拽 2024-08-11 17:44:25

我们通常在它们前面加上“e”前缀。

public enum EthernetLinkSpeed
{
    e10BaseT,
    e100BaseT,
    e1000BaseT,
    Disconnected
}

我们处理很多视频分辨率,720p 等,所以我们使用 e720p。就您而言,我认为使用以太网、快速以太网和千兆以太网这些名称可能会更好。

We usually prefix them with 'e'

public enum EthernetLinkSpeed
{
    e10BaseT,
    e100BaseT,
    e1000BaseT,
    Disconnected
}

We deal with a lot of video resolutions, 720p etc, so we use e720p. In your case, I think using the names may be better, Ethernet, FastEthernet, and GigabitEthernet.

ぺ禁宫浮华殁 2024-08-11 17:44:25

正如您所注意到的,名称很重要。因此,有时探索非显而易见的替代方案是件好事。

一个完全不同的替代方案:

public enum EthernetLinkSpeed {

  [Description("10BASE-T")]
  MegabitSlow,

  [Description("100BASE-T")]
  MegabitFast,

  [Description("1000BASE-T")]
  Gigabit,

  [Description("Disconnected")]
  Disconnected
}

As you've noticed, names are important. So, sometimes it's good to explore non-obvious alternatives.

A completely different alternative:

public enum EthernetLinkSpeed {

  [Description("10BASE-T")]
  MegabitSlow,

  [Description("100BASE-T")]
  MegabitFast,

  [Description("1000BASE-T")]
  Gigabit,

  [Description("Disconnected")]
  Disconnected
}
救赎№ 2024-08-11 17:44:25

为所有相关的枚举添加唯一的前缀总是没有坏处的。如果不出意外的话,它有助于以后对源代码进行搜索/替换编辑。

It never hurts to add a unique prefix to all the related enums. If nothing else, it helps with search/replace editing of your source code down the road.

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