none
Enum names and values RRS feed

  • Question

  • Hi,

    I have some code

    public enum AudioFramesPerSecond : int { _1 = 0, _2 = 2, _4 = 4, _8 = 8, _16 = 16 };        
    
    static void Main(string[] args)        
    {
    foreach (AudioFramesPerSecond value in Enum.GetValues(typeof(AudioFramesPerSecond)))
    {  }            
    foreach (string name in Enum.GetNames(typeof(AudioFramesPerSecond)))
    {  }
    
    }

    Each time I get the NAME of the enumerated item (_1 _2..).
    What I expected: when I ask value, then the list of integer values (1, 2, 4, 8..).
    when name, then names like _1, _2 ...

    Now I have always only names = _1..

    Bug?

    Tuesday, July 2, 2019 10:51 PM

Answers

  • It's because you are defining "value" as an AudioFramesPerSecond. Try defining it as an int.

             foreach (int value in Enum.GetValues(typeof(AudioFramesPerSecond)))
             {
                Console.WriteLine("{0}", value);
             }
    

    • Marked as answer by RobbKirk Tuesday, July 2, 2019 11:34 PM
    Tuesday, July 2, 2019 11:11 PM