C# Class with enumeration or enum type field

Program

using System;

namespace ClassRoomSamples
{
    class Demo03Enumeration
    {
        public enum ShiftType { India = 1, UK, US }
        //By default it will start like {0,1,2}
        //But we have changed starting value to 1, so,
        //it will start like {1,2,3}

        class employee
        {
            public string Name;
            //sft can be 1-India, 2-US, 2-US
            public ShiftType shift;
        }

        static void Main(string[] args)
        {
            //array of object reference variables of employee type
            employee[] emps = new employee[3]; 

            for (int i = 0; i < 3; i++)
            {
                emps[i] = new employee();   

                Console.WriteLine("Enter Name of the employee");
                emps[i].Name = Console.ReadLine();
                Console.WriteLine("Enter the shift code 
                                       (1.India or 2.UK or 3.USA)");
                emps[i].shift = (ShiftType)int.Parse(Console.ReadLine());
            }

            Console.WriteLine("{0,-20}{1,6}", "Name", "Shift");
            foreach (employee x in emps)
            {
                Console.WriteLine("{0,-20}{1,6}", x.Name, x.shift);
            }
            Console.ReadKey();
        }
    }

}
Output

Enter Name of the employee
Syed
Enter the shift code (1.India or 2.UK or 3.USA)
1

Enter Name of the employee
Ponnusamy
Enter the shift code (1.India or 2.UK or 3.USA)
3

Enter Name of the employee
Lakshman
Enter the shift code (1.India or 2.UK or 3.USA)
2

Name                 Shift
Syed                 India
Ponnusamy            US
Lakshman             UK


No comments:

Post a Comment