本文介绍了如何以某种方式定义枚举类型在C#中float或double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它说,的可能类型为枚举字节为sbyte USHORT INT UINT ULONG

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

如果我需要一个浮动双击来定义百分比递增,如 1.5 2.5 例如?我是坚持

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?

由于在这里说:

这是枚举是一组静态常量的结构。之所以
遵循这个准则,因为你会得到一些额外的编译器
和反思的支持,如果你定义一个静态常量枚举与手动定义
A结构。

由于枚举是一组常量,我为什么不能有浮动的常量?

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

更​​新:据说这里:

你知道吗,在CLR支持具有基础类型的枚举float或double尽管大多数语言不选择公开吗?

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?"

由于我只使用C#有没有办法用一些黑客这样做呢?

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

推荐答案

虽然CLR本身支持浮点枚举,C#的设计者没有选择的语言让本(见的)。您可以使用常量作为约翰·桑德斯的回答,或者你可以乘值定义一个整数枚举,然后除以他们回来,如果/当你需要的值。

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?

这篇关于如何以某种方式定义枚举类型在C#中float或double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:21