查找枚举中的最高值
我正在编写一个方法来确定 .NET 枚举中的最高值,以便我可以为每个枚举值创建一个具有一位的 BitArray:
pressedKeys = new BitArray(highestValueInEnum<Keys>());
我需要在两个不同的枚举上使用此方法,因此我将其转换为通用方法:
/// <summary>Returns the highest value encountered in an enumeration</summary>
/// <typeparam name="EnumType">
/// Enumeration of which the highest value will be returned
/// </typeparam>
/// <returns>The highest value in the enumeration</returns>
private static int highestValueInEnum<EnumType>() {
int[] values = (int[])Enum.GetValues(typeof(EnumType));
int highestValue = values[0];
for(int index = 0; index < values.Length; ++index) {
if(values[index] > highestValue) {
highestValue = values[index];
}
}
return highestValue;
}
正如你可以的看,我将 Enum.GetValues() 的返回值转换为 int[],而不是 EnumType[]。这是因为我无法稍后将 EnumType (这是一个泛型类型参数)强制转换为 int 。
该代码有效。但这有效吗? 我是否可以将 Enum.GetValues() 的返回值转换为 int[]?
I'm writing a method which determines the highest value in a .NET enumeration so I can create a BitArray with one bit for each enum value:
pressedKeys = new BitArray(highestValueInEnum<Keys>());
I need this on two different enumerations, so I turned it into a generic method:
/// <summary>Returns the highest value encountered in an enumeration</summary>
/// <typeparam name="EnumType">
/// Enumeration of which the highest value will be returned
/// </typeparam>
/// <returns>The highest value in the enumeration</returns>
private static int highestValueInEnum<EnumType>() {
int[] values = (int[])Enum.GetValues(typeof(EnumType));
int highestValue = values[0];
for(int index = 0; index < values.Length; ++index) {
if(values[index] > highestValue) {
highestValue = values[index];
}
}
return highestValue;
}
As you can see, I'm casting the return value of Enum.GetValues() to int[], not to EnumType[]. This is because I can't cast EnumType (which is a generic type parameter) to int later.
The code works. But is it valid?
Can I always cast the return from Enum.GetValues() to int[]?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,您无法安全地转换为
int[]
。枚举类型并不总是使用int
作为基础值。如果您将自己限制为确实具有int
基础类型的枚举类型,那么应该没问题。如果您愿意,这感觉就像您(或我)可以扩展Unconstrained Melody来支持 -以一种在编译时真正将类型限制为枚举类型的方式,并且适用于任何枚举类型,即使是那些具有底层基础的类型,例如
long
和乌龙
。如果没有 Unconstrained Melody,您仍然可以使用所有枚举类型有效地适当实现
IComparable
的事实以通用方式执行此操作。如果您使用 .NET 3.5,那么它只是一行:No, you can't safely cast to
int[]
. Enum types don't always useint
as an underlying value. If you restrict yourself to enum types which do have an underlying type ofint
, it should be fine though.This feels like something you (or I) could extend Unconstrained Melody to support if you wanted - in a way which genuinely constrained the type to be an enum type at compile time, and worked for any enum type, even those with underlying bases such as
long
andulong
.Without Unconstrained Melody, you can still do this in a general way using the fact that all the enum types effectively implement
IComparable
appropriately. If you're using .NET 3.5 it's a one-liner:根据 Jon Skeet 的建议(也谢谢你,slugster),这是更新后的代码,现在使用 IComparable,因为我仍然以 .NET 2.0 为目标。
对于任何获取代码的人,您可能需要添加额外的检查,这样它就不会在空枚举上崩溃。
As per Jon Skeet's advice (and thank you too, slugster), this is the updated code, now using IComparable because I'm still targeting .NET 2.0.
For anyone grabbing the code, you might want to add an additional check so it doesn't blow up on empty enums.
使用一些现代 C# 功能,我们可以更优雅地做到这一点:
Using some modern C# features we can do this more elegantly:
简单的!使用 LINQ,您的循环可以替换为两行代码,如果您想将其全部合并在一起,则只需一行代码即可。
Easy! Using LINQ your loop can be replaced with two lines of code, or just one if you want to munge it all together.