VS2010 / Framework 4.0 - 获取枚举的最大值

时间:2023-01-26 15:06:54

I'm looking to get the min and max value of an enum not the max amount of enumerators. I've seen Getting the max value of an enum

我希望获得枚举的最小值和最大值,而不是枚举数的最大值。我已经看到获得枚举的最大值

However, in Framework 4, cast() doesn't exist for GetValues().

但是,在Framework 4中,GetValues()不存在cast()。

Example:

例:

public enum Foo
{
     Bird = 1,
     Cat = 2,
     Dog = 4,
     Hampster = 8
}

var last = Enum.GetValues(typeof(Foo)).Cast<int>().Max();

Result should be: 8

Anyone know how this is done in framework 4 without having to enumerate through it all myself?

任何人都知道如何在框架4中完成这项工作,而不必自己枚举所有内容?

1 个解决方案

#1


4  

IEnumerable.Cast<T>() is an extension method and was introduced in .NET 3.5

IEnumerable.Cast ()是一种扩展方法,在.NET 3.5中引入

Are you missing a using System.Linq; statement?

你错过了使用System.Linq;声明?

You need that namespace to find the extension method.

您需要该命名空间来查找扩展方法。

#1


4  

IEnumerable.Cast<T>() is an extension method and was introduced in .NET 3.5

IEnumerable.Cast ()是一种扩展方法,在.NET 3.5中引入

Are you missing a using System.Linq; statement?

你错过了使用System.Linq;声明?

You need that namespace to find the extension method.

您需要该命名空间来查找扩展方法。