C# Class implementation | C# Class Sample Program

Program

using System;

namespace ClassRoomSamples
{
    class Demo15ClassExample
    {
        class trainee
        {
            //Private fileds (Also called as instance variables)
            int _traineeID;
            string _traineeName;
            int _traineeScore;
            string _traineeResult;

            //public Methods
            public void getDetails(int traineeID, string traineeName,int                                                              traineeScore)
            {
                //Here thease traineeID, traineeName, traineeScore are                                          called as local variables
                _traineeID = traineeID;
                _traineeName = traineeName;
                _traineeScore = traineeScore;

                if (_traineeScore >= 7)
                    _traineeResult = " Selected";
                else
                    _traineeResult = " Not Selected";
            }
            public void disDetails()
            {
                Console.WriteLine("{0,-10}{1,-20}{2,8}{3,-15}",                             _traineeID, _traineeName, _traineeScore, _traineeResult);
            }
        };

        static void Main(String[] anyName)
        {
            //Only array of references of trainee type 
            //will be created here not actual objects..

            trainee[] t = new trainee[3];  

            for (int i = 0; i < 3; i++)
            {
                //Actual objects will be created here based on demand
                t[i] = new trainee();

                Console.WriteLine("Enter Trainee ID");
                int traineeID = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter Name of the trainee");
                string traineeName = Console.ReadLine();

                Console.WriteLine("Enter Assessment score");
                int traineeScore = int.Parse(Console.ReadLine());

                t[i].getDetails(traineeID,traineeName,traineeScore);
            }

            Console.WriteLine("==============================");
            Console.WriteLine("{0,-10}{1,-20}{2,8}{3,-15}",  
                                        "ID","Name","Score"," Result");
            Console.WriteLine("===============================");
            for (int i = 0; i < 3; i++)
            {
                t[i].disDetails();
            }

            Console.ReadKey();
        }
    }
}


Output

Enter Trainee ID
802351
Enter Name of the trainee
Shalini
Enter Assessment score
8

Enter Trainee ID
802352
Enter Name of the trainee
Neha Yadav
Enter Assessment score
7

Enter Trainee ID
802353
Enter Name of the trainee
Rajesh
Enter Assessment score
6

===================================================
ID        Name             Score  Result
===================================================
802351    Shalini               8 Selected
802352    Neha Yadav            7 Selected
802353    Rajesh                6 Not Selected

No comments:

Post a Comment