Monday 10 February 2014

Enums in C#

                                                                                                                                                   Previous...
                                                                                                                                                        Next...


C# enumerations are value type.  An enumerated type is declared  the enum keyword.

Declaring enum keyword 

enum <enum_name> 
{ 
    enumeration list 
};
Where,
  • The enum_name specifies the enumeration type name.
  • The enumeration list is a comma-separated list of identifiers.
 enumeration stands for an integer value, one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0. For example:
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

Example:

The following example demonstrates use of enum variable:
using System;
namespace enumexample
{
   class EnumProgram
   {
      enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

      static void Main(string[] args)
      {
         int WeekStart = (int)Days.Mon;
         int WeekEnd =   (int)Days.sunday;
         Console.WriteLine("Monday: {0}", WeekStart);
         Console.WriteLine("sunday: {0}", WeekEnd);
         Console.Readline();
      }
   }
}
result
Monday: 1
Friday: 7


                                                                                                                                                   Previous...
                                                                                                                                                        Next...

No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...