Getting the max value of an enum

后端 未结 11 2287
旧时难觅i
旧时难觅i 2020-12-07 13:42

How do you get the max value of an enum?

相关标签:
11条回答
  • 2020-12-07 14:03

    I used the following when I needed the min and max values of my enum. I just set a min equal to the lowest value of the enumeration and a max equal to the highest value in the enumeration as enum values themselves.

    public enum ChannelMessageTypes : byte
    {
        Min                 = 0x80, // Or could be: Min = NoteOff
        NoteOff             = 0x80,
        NoteOn              = 0x90,
        PolyKeyPressure     = 0xA0,
        ControlChange       = 0xB0,
        ProgramChange       = 0xC0,
        ChannelAfterTouch   = 0xD0,
        PitchBend           = 0xE0,
        Max                 = 0xE0  // Or could be: Max = PitchBend
    }
    
    // I use it like this to check if a ... is a channel message.
    if(... >= ChannelMessageTypes.Min || ... <= ChannelMessages.Max)
    {
        Console.WriteLine("Channel message received!");
    }
    
    0 讨论(0)
  • 2020-12-07 14:08

    This is slightly nitpicky but the actual maximum value of any enum is Int32.MaxValue (assuming it's a enum derived from int). It's perfectly legal to cast any Int32 value to an any enum regardless of whether or not it actually declared a member with that value.

    Legal:

    enum SomeEnum
    {
        Fizz = 42
    }
    
    public static void SomeFunc()
    {
        SomeEnum e = (SomeEnum)5;
    }
    
    0 讨论(0)
  • 2020-12-07 14:13

    In agreement with Matthew J Sullivan, for C#:

       Enum.GetValues(typeof(MyEnum)).GetUpperBound(0);
    

    I'm really not sure why anyone would want to use:

       Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Last();
    

    ...As word-for-word, semantically speaking, it doesn't seem to make as much sense? (always good to have different ways, but I don't see the benefit in the latter.)

    0 讨论(0)
  • 2020-12-07 14:13

    There are methods for getting information about enumerated types under System.Enum.

    So, in a VB.Net project in Visual Studio I can type "System.Enum." and the intellisense brings up all sorts of goodness.

    One method in particular is System.Enum.GetValues(), which returns an array of the enumerated values. Once you've got the array, you should be able to do whatever is appropriate for your particular circumstances.

    In my case, my enumerated values started at zero and skipped no numbers, so to get the max value for my enum I just need to know how many elements were in the array.

    VB.Net code snippets:

    '''''''
    
    Enum MattType
      zerothValue         = 0
      firstValue          = 1
      secondValue         = 2
      thirdValue          = 3
    End Enum
    
    '''''''
    
    Dim iMax      As Integer
    
    iMax = System.Enum.GetValues(GetType(MattType)).GetUpperBound(0)
    
    MessageBox.Show(iMax.ToString, "Max MattType Enum Value")
    
    '''''''
    
    0 讨论(0)
  • 2020-12-07 14:20

    Enum.GetValues() seems to return the values in order, so you can do something like this:

    // given this enum:
    public enum Foo
    {
        Fizz = 3, 
        Bar = 1,
        Bang = 2
    }
    
    // this gets Fizz
    var lastFoo = Enum.GetValues(typeof(Foo)).Cast<Foo>().Last();
    

    Edit

    For those not willing to read through the comments: You can also do it this way:

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

    ... which will work when some of your enum values are negative.

    0 讨论(0)
提交回复
热议问题