C# Class Implementation instead of structure

using System;

namespace ClassRoomSamples
{
    class Demo13AlternatetoStructure
    {
        //Class is a reference type
        class trainee 
        {
            public string traineeName;
            public int score;
            public string result;
        };

        static void Main(string[] args)
        {
            //Only array of references will be created here.
            trainee[] t = new trainee[3];   

            for (int i = 0; i < 3; i++)
            {
                //Actual object is created here only on demand
                t[i] = new trainee(); 

                Console.WriteLine("Enter Name, score of participants");
                t[i].traineeName = Console.ReadLine();
                t[i].score = int.Parse(Console.ReadLine());

                if (t[i].score >= 7)
                    t[i].result = "Selected";
                else
                    t[i].result = "Not Selected";
            }

            Console.WriteLine("=====================================");
            Console.WriteLine("Name\tScore\tResult");
            Console.WriteLine("=====================================");
            for (int i = 0; i < 3; i++)
            {
                Console.Write(t[i].traineeName + "\t");
                Console.Write(t[i].score + "\t");
                Console.WriteLine(t[i].result);
            }

            Console.ReadKey();
        }
    }

}

Output

Enter Name, score of participants
chitra
8
Enter Name, score of participants
Abinesh
9
Enter Name, score of participants
Akash
7
=====================================
Name    Score   Result
=====================================
chitra  8       Selected
Abinesh 9       Selected
Akash   7       Selected

No comments:

Post a Comment