在枚举中找到最高价值 [英] Finding the Highest Value in an Enumeration

查看:152
本文介绍了在枚举中找到最高价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个方法来确定.NET枚举中的最高值,所以我可以为每个枚举值创建一个BitArray:

  pressedKeys = new BitArray(highestValueInEnum< Keys>()); 

我需要这两个不同的枚举,所以我把它变成一个通用的方法:

  ///< summary>返回枚举中遇到的最高值< / summary> 
///< typeparam name =EnumType>
///将返回最高值的枚举
///< / typeparam>
///< returns>枚举中的最大值< / returns>
private static int highestValueInEnum&EnumType>(){
int [] values =(int [])Enum.GetValues(typeof(EnumType));
int highestValue = values [0]; (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 []?

解决方案

不,你可以不安全地转换为 int [] 。枚举类型并不总是使用 int 作为底层值。如果你限制自己的枚举类型,其中具有底层类型 int ,那么应该是罚款。



这感觉像你(或我)可以扩展的内容无约束的旋律支持,如果你想要的 - 在某种程度上,在编译时真正地将类型限制为枚举类型,并且为任何枚举类型工作,甚至具有基础基础的类型,例如 long ulong



没有无约束的旋律,你仍然可以这样做使用所有枚举类型有效地实现 IComparable 的事实的一般方式。如果你使用的是.NET 3.5,它是一个单行:

  private static TEnum GetHighestValue< TEnum>(){
return Enum.GetValues(typeof(TEnum))。Cast< TEnum>()。Max();
}


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[]?

解决方案

No, you can't safely cast to int[]. Enum types don't always use int as an underlying value. If you restrict yourself to enum types which do have an underlying type of int, 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 and ulong.

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:

private static TEnum GetHighestValue<TEnum>() {
  return Enum.GetValues(typeof(TEnum)).Cast<TEnum>().Max();
}

这篇关于在枚举中找到最高价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆