Description:

Syntax

enum name_of_enum {enumeration list };

Example

  1. class demo_enum
  2. {
  3. enum my_enum
  4. {
  5. a, b, c, d, e, f
  6. };
  7. public static void Main()
  8. {
  9. my_enum i; // declare an enum variable
  10. for (i = my_enum.a; i < my_enum.f; i++) Console.WriteLine(" {0}", i + " has value of: " + (int) i);
  11. Console.WriteLine();
  12. Console.ReadKey();
  13. }
  14. }
Output:

console

Some Rules for Enum

  1. Initializing the enum member is not allow,
    1. enum Rules
    2. {
    3. a, b, c = d, d, e, f
    4. };
    5. public static void Main()
    6. {
    7. Rules i; // declare an enum variable
    8. for (i = Rules.a; i < Rules.f; i++) Console.WriteLine(" {0}", i + " has value of: " + (int) i);
    9. Console.WriteLine();
    10. Console.ReadKey();
    11. }
    c = d is not allow

  2. Another variable declaration with same name is allow, but can’t create using new keyword.
    1. enum Rules
    2. {
    3. a, b, c, d, e, f
    4. };
    5. public static void Main()
    6. {
    7. int d = 2500; // another variable declared
    8. // new int e = 100; // error - A new expression requires
    9. Rules i; // declare an enum variable
    10. for (i = Rules.a; i < Rules.f; i++)
    11. {
    12. Console.WriteLine(" {0}", i + " has value of: " + (int) i);
    13. Console.WriteLine();
    14. }
    15. Console.WriteLine(" variable d has value of: " + d);
    16. Console.ReadKey();
    17. }
    new int e = 100; will get an error - A new expression requires

    output

  3. Value of enum can’t increment directly,
    1. enum Rules
    2. {
    3. a, b, c, d, e, f
    4. };
    5. public static void Main()
    6. {
    7. a++; // error - can not change the value of constant
    8. Rules i; // declare an enum variable
    9. for (i = Rules.a; i < Rules.f; i++)
    10. {
    11. Console.WriteLine(" {0}", i + " has value of: " + (int) i);
    12. Console.WriteLine();
    13. }
    14. Console.ReadKey();
    15. }
    a++; will get an error – can’t change the value of constant

  4. Use of enum for initialization of another variable,
    1. enum Rules
    2. {
    3. a, b, c, d, e, f
    4. };
    5. public static void Main()
    6. {
    7. int z = Convert.ToInt16(Rules.b); //// use enum for use initialization of another variable
    8. Rules i; // declare an enum variable
    9. for (i = Rules.a; i < Rules.f; i++)
    10. {
    11. Console.WriteLine(" {0}", i + " has value of: " + (int) i);
    12. Console.WriteLine();
    13. }
    14. Console.WriteLine(" variable d has value of: " + z);
    15. Console.ReadKey();
    16. }
    result

  5. Size of enum can be decide at the time time of declaration enum,
    1. enum Rules
    2. {
    3. a, b, c, d, e, f
    4. };
    5. enum short_values: short
    6. {
    7. B1 = 01, B2 = 02
    8. }
    9. enum long_values: long
    10. {
    11. A1 = 123456789985, A2 = 45678954123
    12. }
    13. public static void Main()
    14. {
    15. Rules i; // declare an enum variable
    16. Console.WriteLine(" size of enum Rules has value of : {0} ", sizeof(Rules));
    17. Console.WriteLine(" size of enum short_values has value of : {0} ", sizeof(short_values));
    18. Console.WriteLine(" size of enum long_values has value of : {0} ", sizeof(long_values));
    19. Console.ReadKey();
    20. }
    run

Summary

This blog will help fresher candidates to understand enum in depth.