·  C# - Syntax - Collections - Enum

 

- Info:

  - Enum is collection of CONSTANT variables which can be of any type except char and default being int.

  - If value is not defined member will have value which is one more then the value of previous member.

  - Default value of first member is 0.

  - Enum must be declared inside class outside any member functions.

  - Whenever you use enum variable you have to explicitly cast it to declared type/

 

- Example:

  - Create C# Console Application Test.cs.

 

    Test.cs

    using System;

   

    class Test {

   

      //                    0    -10  9    20 21 22    20 21

      enum Sequence :long { a, b=-10, c, d=20, e, f, g=20, h };

   

      static void Main(string[] args) {   

   

        Console.WriteLine("a = " + (long) Sequence.a);  //Explicit conversion is ALWAYS needed.

        Console.WriteLine("b = " + (long) Sequence.h);  //even if default :int is used.

   

      }

   

    }