//************************************** // Name: C# enum example // Description:A very simple demonstration of how easy ENUMs can be. To test this code, create a console app, copy and paste it, then run it. // By: Dave Bowlin // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //************************************** /* Enum example * * enums are used to declare a list of named integer constants * which you can reference using the name, or the integer value * * as usual, when counting indexes in C#, the number starts at zero * but you can change the starting index number by assigning a value to it * * example: public enum Colors { Green = 25, Red, Blue, Orange } */ using System; namespace enum_example { public enum Colors { Green, Red, Blue, Orange, Yellow, Pink, Brown, White, Black, Teal, Wheat, Transparent} // our enum, which holds named colors class Program { static void Main(string[] args) { Random r = new Random(); int i = Enum.GetValues(typeof(Colors)).Length; // get and display how many indexes in our Days enum Console.WriteLine("Number of colors: " + i); Array values = Enum.GetValues(typeof(Colors)); // put the enum values into an array for (int x = 0; x <= i; x++) { Colors color = (Colors)values.GetValue(r.Next(values.Length)); // select random day Console.WriteLine(x + ": " + color + ", index: " + ((int)color)) ; // display the random color, and the index } Console.ReadKey(); } } }