Enum type in C# : How to

An enum type to define a set of named integral constants .

Examples:
   public  enum nums { zero, one, two, three, four, five, six, seven, eight, nine, teen };
  • enum: A keyword which denotes enum type
  • nums: This is a type which has defined elements (such as zero, one....)
  • {zero, one ...} are elements which has the underlying type of each element (such as int, string, long...) . By default, whose type is int
The following example will demonstrate the features of enum type

   class Program
    {
        enum nums { zero, one, two, three, four, five, six, seven, eight, nine, teen };

        static void Main(string[] args)
        {
          
            nums da = nums.one ;

            da = increment(da);
            Console.WriteLine(da);
             Console.ReadKey();
          
        }
        private static nums increment(nums pnums)
        {
            if (pnums == nums.eight)
                return pnums;
            else
                return pnums+1;
                    }

        private static nums decrement(nums pnums)
        {
            if (pnums == nums.one)
                return pnums;
            else
                return pnums - 1;
        }
    }
Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment