如何在 C# 中将枚举类型定义为 float 或 double

发布于 2024-12-19 12:36:03 字数 1039 浏览 1 评论 0原文

此处表示枚举的可能类型 是 bytesbyteshortushortintuintlongulong

如果我需要 floatdouble 来定义百分比增量(例如 1.52.5)怎么办?我被困住了吗?

正如这里所说: http://en.csharp-online.net/.NET_Type_Design_Guidelines%E2%80 %94Enum_Design

枚举是具有一组静态常量的结构。原因是 遵循这个指南是因为你会得到一些额外的编译器 和反射支持(如果您定义枚举而不是手动定义) 具有静态常量的结构。

既然枚举是一组常量,为什么我不能有浮点常量?

更新:这里说的是: http://en.csharp-online.net/.NET_Type_Design_Guidelines%E2%80 %94Enum_Design “您是否知道 CLR 支持具有 float 或 double 基础类型的枚举,尽管大多数语言不选择公开它?”

由于我只使用 c#,有没有办法通过一些技巧来做到这一点?

It says here that the possible types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

What if I need a float or a double to define percentage increments such as 1.5 or 2.5 for example? Am I stuck?

As said here:
http://en.csharp-online.net/.NET_Type_Design_Guidelines%E2%80%94Enum_Design

An enum is a structure with a set of static constants. The reason to
follow this guideline is because you will get some additional compiler
and reflection support if you define an enum versus manually defining
a structure with static constants.

Since an enum is a set of constants, why can't I have float constants ?

Update: it is said here:
http://en.csharp-online.net/.NET_Type_Design_Guidelines%E2%80%94Enum_Design
"Did you know that the CLR supports enums with an underlying type of float or double even though most languages don't choose to expose it?"

Since I'm only using c# is there a way to do so with some hacks ?

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

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

发布评论

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

评论(3

醉南桥 2024-12-26 12:36:03

尽管 CLR 本身支持浮点枚举,但 C# 设计者选择不在语言中公开这一点(请参阅 http://en.csharp-online.net/.NET_Type_Design_Guidelines%E2%80%94Enum_Design)。您可以像 John Saunders 的答案中那样使用常量,也可以定义一个具有相乘值的整数枚举,然后在需要该值时将它们除回去。

不过,用例肯定会很有趣。你为什么需要/想要这个?

Although the CLR itself supports floating point enums, C# designers chose not to expose this in the language (see http://en.csharp-online.net/.NET_Type_Design_Guidelines%E2%80%94Enum_Design). You can either use constants as in John Saunders' answer, or you can define an integer enum with multiplied values and then divide them back if/when you need the value.

The use case would be definitely interesting, though. Why do you need/want this?

可是我不能没有你 2024-12-26 12:36:03

是的,你被困在那里了。您不能将enum与浮点类型一起使用。但是,您可以将静态类与常量一起使用:

public static class MyFloatEnum {
    public const float One = 1.0;
    public const float OneAndAHalf = 1.5;
    // etc.
}

并且它在 IntelliSense 中看起来有些接近。或者,您可能只想使用常量:

public const float A = 0.5;
public const float B = 17.62;

Yes, you're stuck there. You can't use enums with floating-point types. You can use a static class with constants, however:

public static class MyFloatEnum {
    public const float One = 1.0;
    public const float OneAndAHalf = 1.5;
    // etc.
}

And it will look somewhat close in IntelliSense. Alternatively, you may just want to use constants:

public const float A = 0.5;
public const float B = 17.62;
兔姬 2024-12-26 12:36:03

您将必须使用一组常量:

public const float Percentage1 = 1.5;
public const float Percentage2 = 2.5;

You will have to use a set of constants:

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