C# Enumerations Part 2


Here’s an enumumeration to keep track of the scores for tricks at a dog competition:

namespace EnumsHeadFirst 
{   // code in Beginning C# 6 book - Ch 05 folder
    // keep track of the scores for tricks
    // at a dog competition.
    public enum TrickScore  { 
        Sit = 7, Beg = 25, RollOver = 50,
        Fetch = 10, ComeHere = 5, Speak = 30,
    }
    class Program {
        static void Main(string[] args)  {
            // output: Rollover
            WriteLine(TrickScore.RollOver);
            int myInt = (int)TrickScore.Fetch * 3;
            WriteLine(myInt.ToString()); // output: 30
            TrickScore myScore = (TrickScore)myInt;
            WriteLine(myScore.ToString()); // output: Speak
            ReadKey();
        }
    }
}

You can cast an int to an enum, and you can cast an (int-based) enum back to an int.

Leave a comment

Your email address will not be published. Required fields are marked *