Mat*_*ton 201
Enum.GetValues()似乎按顺序返回值,因此您可以执行以下操作:
// given this enum:
public enum Foo
{
Fizz = 3,
Bar = 1,
Bang = 2
}
// this gets Fizz
var lastFoo = Enum.GetValues(typeof(Foo)).Cast<Foo>().Last();
Run Code Online (Sandbox Code Playgroud)
编辑
对于那些不愿意阅读评论的人:你也可以这样做:
var lastFoo = Enum.GetValues(typeof(Foo)).Cast<Foo>().Max();
Run Code Online (Sandbox Code Playgroud)
...当你的一些枚举值为负数时,它会起作用.
Kar*_*ang 37
我同意马特的回答.如果只需要min和max int值,则可以按如下方式执行.
最大值:
Enum.GetValues(typeof(Foo)).Cast<int>().Max();
Run Code Online (Sandbox Code Playgroud)
最低:
Enum.GetValues(typeof(Foo)).Cast<int>().Min();
Run Code Online (Sandbox Code Playgroud)
Shi*_*mmy 20
根据Matt Hamilton的回答,我想为它创建一个Extension方法.
由于ValueType
不接受作为泛型类型参数约束,我没有找到更好的方法限制T
,Enum
但以下.
任何想法都会非常感激.
PS.请忽略我的VB隐含性,我喜欢用这种方式使用VB,这就是VB的优点,这就是我喜欢VB的原因.
Howeva,这里是:
static void Main(string[] args)
{
MyEnum x = GetMaxValue<MyEnum>();
}
public static TEnum GetMaxValue<TEnum>()
where TEnum : IComparable, IConvertible, IFormattable
//In C#>=7.3 substitute with 'where TEnum : Enum', and remove the following check:
{
Type type = typeof(TEnum);
if (!type.IsSubclassOf(typeof(Enum)))
throw new
InvalidCastException
("Cannot cast '" + type.FullName + "' to System.Enum.");
return (TEnum)Enum.ToObject(type, Enum.GetValues(type).Cast<int>().Last());
}
enum MyEnum
{
ValueOne,
ValueTwo
}
Run Code Online (Sandbox Code Playgroud)
Public Function GetMaxValue _
(Of TEnum As {IComparable, IConvertible, IFormattable})() As TEnum
Dim type = GetType(TEnum)
If Not type.IsSubclassOf(GetType([Enum])) Then _
Throw New InvalidCastException _
("Cannot cast '" & type.FullName & "' to System.Enum.")
Return [Enum].ToObject(type, [Enum].GetValues(type) _
.Cast(Of Integer).Last)
End Function
Run Code Online (Sandbox Code Playgroud)
Jar*_*Par 13
这有点挑剔,但任何枚举的实际最大值是Int32.MaxValue(假设它是从int派生的枚举).将任何Int32值转换为任何枚举是完全合法的,无论它是否实际声明具有该值的成员.
法律:
enum SomeEnum
{
Fizz = 42
}
public static void SomeFunc()
{
SomeEnum e = (SomeEnum)5;
}
Run Code Online (Sandbox Code Playgroud)
小智 9
经过另一次尝试,我得到了这个扩展方法:
public static class EnumExtension
{
public static int Max(this Enum enumType)
{
return Enum.GetValues(enumType.GetType()).Cast<int>().Max();
}
}
class Program
{
enum enum1 { one, two, second, third };
enum enum2 { s1 = 10, s2 = 8, s3, s4 };
enum enum3 { f1 = -1, f2 = 3, f3 = -3, f4 };
static void Main(string[] args)
{
Console.WriteLine(enum1.one.Max());
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
使用Last函数无法获得最大值.使用"max"功能即可.喜欢:
class Program
{
enum enum1 { one, two, second, third };
enum enum2 { s1 = 10, s2 = 8, s3, s4 };
enum enum3 { f1 = -1, f2 = 3, f3 = -3, f4 };
static void Main(string[] args)
{
TestMaxEnumValue(typeof(enum1));
TestMaxEnumValue(typeof(enum2));
TestMaxEnumValue(typeof(enum3));
}
static void TestMaxEnumValue(Type enumType)
{
Enum.GetValues(enumType).Cast<Int32>().ToList().ForEach(item =>
Console.WriteLine(item.ToString()));
int maxValue = Enum.GetValues(enumType).Cast<int>().Max();
Console.WriteLine("The max value of {0} is {1}", enumType.Name, maxValue);
}
}
Run Code Online (Sandbox Code Playgroud)