C# 枚举加法
我有以下枚举:
public enum LegalShipTypes : byte
{
Frigate = 1,
Cruiser = 2,
Destroyer = 3,
Submarine = 4,
AircraftCarrier = 5
}
有没有办法以任何方式获取枚举的总价值?例如,这将导致 (1 + 2 + 3 + 4 + 5) = 15。
I have the following enum:
public enum LegalShipTypes : byte
{
Frigate = 1,
Cruiser = 2,
Destroyer = 3,
Submarine = 4,
AircraftCarrier = 5
}
Is there is a way to get the total value of enum in any way? For instance, this would result in (1 + 2 + 3 + 4 + 5) = 15.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您可以编辑枚举,并且在许多地方需要它们的总和,则可以将其放在枚举本身中:
但这在标志枚举中更有意义:
或者您可以只使用它:
它返回一个
小数.
但这是一种更通用的方法(无论枚举的基础类型如何):
If you can edit the enum, and you need their sum in many places, you can put it in the enum itself:
This makes more sense in flags enums though:
Or you can just use this:
Which returns a
decimal
.But this is a more general way to do it (works regardless of the underlying type of the enum):
我不想将其作为答案输入,因为它不会直接回答您的问题,但根据您对我对您问题的评论的评论,它值得一些解释。
枚举是非常简单的类型安全的状态表示。如果您只是使用常量,那么您可能会将错误的常量分配给一个值。这可以防止您将错误类型的常量分配给字段。例如,如果您有需要 DayOfWeek 的内容,则无法分配 FileAccess 值,即使它们都是同一基础类型的常量。
如果您需要这种类型安全并且您不需要枚举表现出任何其他类型的行为,那么请使用枚举。如果您担心让枚举也执行其他操作(例如枚举、数学运算等),那么您应该考虑使用类。请参阅下面我的示例。
现在您可以以类型安全的方式使用它,如下所示:
如您所见,您仍然具有类型安全性,但灵活性更高。它是更多的代码,但您不会发现自己正在克服枚举的限制。重申一下,枚举的目的很简单。这应该很简单。如果您的需求很简单,请不要犹豫使用它。如果您的需求更复杂,那么使用良好的老式面向对象编程来解决您的问题并没有什么可耻的。
编辑
根据您最后的评论回复,字节值代表钉子的数量,我强烈建议您不要使用枚举来解决您的问题。你会(讽刺地)试图将圆钉放入方孔中。
I didn't want to type this up as an answer, because it doesn't answer your question directly, but based on your comment in response to my comment to your question, it merits some explanation.
Enums are meant to be very simple type safe representations of state. If you simply use constants, then you can assign the wrong constants to a value. This prevents you from assigning the wrong type of constant to a field. For example, if you have something that expects DayOfWeek, you can't assign a FileAccess value, even though they are both constants of the same underlying type.
If you need this type safety and you don't need for your enum to exhibit any other type of behavior, then use an enum. If you are concerned with having your enum do other things as well (such as enumeration, mathematical operations, etc) then you should consider using classes. See my example below.
Now you can use it in a typesafe way like this:
As you can see, you still have type safety, but much more flexibility. It is more code, but you won't find yourself working against the limitations of enum. To reiterate, enum is meant to be simple. It's supposed to be simple. If your needs are simple, don't hesitate to use it. If your needs are more complex, there's no shame in using good old fashioned object oriented programming to solve your problem.
EDIT
In light of your last comment response that the byte values represents the number of pegs, I would highly recommend you don't use enums to solve your problem. You'd be (ironically) trying to put a round peg in a square hole.
假设它是一个继承自 System.Int32 的枚举(这是默认值),请尝试以下操作。
编辑没有注意到OP的问题继承自
byte
。这是一个适用于byte
的版本Try the following assuming it's an enum that inherits from System.Int32 (this is the default).
EDIT didn't notice the OP's question inherits from
byte
. Here's a version that works withbyte
请参阅此类似问题的已接受答案:
How do I enumerate an enum?
您可以获取值,枚举它们,然后在 for 循环中对值求和。
See the accepted answer of this similar question:
How do I enumerate an enum?
You could get the values, enumerate through them and just sum the values in the for loop.
如果您想对枚举器值求和,那么使用带标记的枚举器不是更好吗?
If you want to be summing enumerator values, wouldn't you be better off using a flagged enumerator?
JaredPar 答案的通用版本与卢克的更正:
致电:
编辑:
好吧,放弃这个想法。我想:
就是答案。
A generic version of JaredPar's answer with Luke's correction:
Call with:
EDIT:
Well, scratch that idea. I suppose:
is the answer.