Enumeration

.Enumeration Example


Enumeration Example Program Output

.Enumeration

"This C# program illustrates the use of the enum type for creating enumerations. Standard enumerations, enumeration flags, common enumeration methods, extension methods, and bitwise operations are used in the program."

The Enumeration program declares enumerations with and without initializers, flag enumerations for bitwise flags, and enumeration subsets. Bi-wise operations are used with the flag enumerations. Extension methods are created to determine if a Color member is "red" or "red or blue". The Enumeration methods GetValues(), and GetName() are used to print out all the constants and values in an enumeration. The bitwise "and" and the IsDefined() method are used to test for enumeration membership. The following C# features are used in the sample code:

  1. enum - used to declare a distinct set of named constants with an underlying integral type.
  2. FlagsAttribute - Indicates that an enumeration can be treated as a bit field; that is, a set of flags.
  3. Enumeration Types - Provides an efficient way to define a set of named integral constants that may be assigned to a variable.
  4. Extension Methods - Enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
  5. Bitwise Operations - Provides for manipulation of data at the bit level.

Enumeration Program

using System;
namespace EnumerationExample
{
    public enum ElementSide { Top, Right, Bottom, Left }
    public enum Days { None, Sun = 1, Mon, Tue, Wed, Thu, Fri }

    public enum LaborCost
    {
        None = 0,
        Intern = 5,
        Trainee = 10,
        Novice = 15,
        Certified = 20,
        Expert = Certified + 5
    }

    // Enum Extension Methods
    public enum Color { Red, White, Blue }
    public static class ColorExtension
    {
        public static bool IsRed(this Color c)
        {
            return c == Color.Red;
        }
        public static bool IsRedOrBlue(this Color c)
        {
            return c == Color.Red || c == Color.Blue;
        }
    }

    // Enumeration as Bit Flags
    [Flags]
    enum AdditiveColors : short
    {
        Black = 0,
        Red = 1,
        Green = 2,
        Blue = 4,
        Yellow = Red | Green,
        Magenta = Red | Blue,
        Cyan = Green | Blue,
        White = Red | Blue | Green
    };

    // Create a Subset of Enumeration
    public enum PrimaryColors
    {
        Red = AdditiveColors.Red,
        Green = AdditiveColors.Green,
        Blue = AdditiveColors.Blue
    };


    class Program
    {
        static void Main()
        {
            Console.WriteLine(" --- Printing Individual Enum Constants and Values ---");
            System.Console.WriteLine("Enum constant name for left side is: {0}",ElementSide.Left); // Prints: Left
            System.Console.WriteLine("Enum value for left side is {0}", (int)ElementSide.Left); // Prints: 3
            System.Console.WriteLine("Enum value for Mon is {0}", (int)Days.Mon); // Prints: 2
            System.Console.WriteLine("Enum value for Expert is {0}", (int)LaborCost.Expert); // Prints: 25
            Console.WriteLine();

            Console.WriteLine(" --- Printing Enum Set of Constants and Values ---");
            // Print Enum Constant and Underlying Value
            foreach (int i in Enum.GetValues(typeof(LaborCost)))
            {
                Console.Write(Enum.GetName(typeof(LaborCost), i));
                Console.WriteLine(" = {0}", i);
            }
            Console.WriteLine();

            Console.WriteLine(" --- Enum Extension Methods ---");
            Color myColor = Color.Blue;
            Console.WriteLine(myColor.IsRed()); // Prints: False
            Console.WriteLine(myColor.IsRedOrBlue()); // Prints: True
            Console.WriteLine();

            Console.WriteLine(" --- Printing Enum Flag Set of Constants and Values ---");
            // Print Enum Flag Constant and Underlying Value
            foreach (short i in Enum.GetValues(typeof(AdditiveColors)))
            {
                Console.Write(Enum.GetName(typeof(AdditiveColors), i));
                Console.WriteLine(" = {0}", i);
            }
            Console.WriteLine();

            Console.WriteLine(" --- Enum Membership Testing ---");
            // Test the Component Colors
            if ((AdditiveColors.Yellow & AdditiveColors.Red) == AdditiveColors.Red)
                Console.WriteLine("Yellow contains the color Red.");

            if ((AdditiveColors.Cyan & AdditiveColors.Red) != AdditiveColors.Red)
                Console.WriteLine("Cyan does Not contains the color Red.");
            Console.WriteLine();

            // Test for Enum Value in Subset
            if (!Enum.IsDefined(typeof(PrimaryColors), (PrimaryColors) AdditiveColors.Cyan))
                Console.WriteLine("Cyan is Not a primary color.");
            Console.WriteLine();
        }
    }
}