Class Example

Let say we have a Company having various employees working in it. so now think of Employee is a class and we have Employee details such as Name, BirthDate, Salary as the fields associated with Employee class and we have methods defined as ComputeAnnualSalary, CalculateTotalPendingLeaves for the Employee class. Now we can show this Employee class in actual class format below(class represented using C# syntax). We use class keyword to create class.

+
    public class Employee
    {
 
        //Fields in class
        public string Name;
        public string BirthDate;
        public int Salary;
 
        int TotalLeaves = 30;
        int OtherIncentives = 5000;
 
        public Employee()
        {
        }
 
        //properties in class
        public int LeavesTaken { getset; }
 
        //Method/function in class
        public int computeSalary()
        {
            return (Salary * 12) + OtherIncentives;
        }
 
        public int CalculatePendingLeaves()
        {
            return TotalLeaves - LeavesTaken;
        }
    }

Now consider each employee is an object. Let say we have employees Sam, Ted, Merry, Ramesh as an object for class Employee and they have different birthdate, Salary, Name associate with them. We can create object of this class using new keyword.
basic syntax is:
            ClassName objectname = new ClassName();
We can initialise the values just as below directly in {} bracket when we create an object.
Now lets creates these objects. for this example create console application and create class file there with above class name and copy paste the above code there. Write the below code in program.cs file of your console appication and run the application.

+

class Program
    {
        //create object of class using new keyword
        Employee sam = new Employee() { Name = "Sam", BirthDate = "25 Oct 1983", Salary = 10000, LeavesTaken = 0 };
        Employee Merry = new Employee() { Name = "Merry", BirthDate = "10 Oct 1983", Salary = 12000, LeavesTaken = 10 };
        Employee Ramesh = new Employee() { Name = "Ramesh", BirthDate = "5 Aug 1985", Salary = 15000, LeavesTaken = 5 };
 
        //variable to store Annual salary and pending leaves of employee
        int SamAnnualSal = 0, SamPendingLeaves = 0;
        int MerryAnnualSal = 0, MerryPendingLeaves = 0;
        int RameshAnnualSal = 0, RameshPendingLeaves = 0;
 
        //function to save annual salary and pending leaves.
        protected void computeAnnulSalAndPendingLeaves()
        {
            //Annual Salary
            SamAnnualSal = sam.computeSalary();
            MerryAnnualSal = Merry.computeSalary();
            RameshAnnualSal = Ramesh.computeSalary();
 
            //Pending Leaves
            SamPendingLeaves = sam.CalculatePendingLeaves();
            MerryPendingLeaves = Merry.CalculatePendingLeaves();
            RameshPendingLeaves = Ramesh.CalculatePendingLeaves();
 
            //print the output with each employee details
            Console.WriteLine("** Sam Details**");
            Console.WriteLine("Name = {0}", sam.Name);
            Console.WriteLine("Birth Date = {0}", sam.BirthDate);
            Console.WriteLine("Monthly Salary = {0}", sam.Salary);
            Console.WriteLine("Annual Package = {0}", SamAnnualSal);
            Console.WriteLine("Pending Leaves = {0}", SamPendingLeaves);
 
            Console.WriteLine("\n** Merry Details**");
            Console.WriteLine("Name = {0}", Merry.Name);
            Console.WriteLine("Birth Date = {0}", Merry.BirthDate);
            Console.WriteLine("Monthly Salary = {0}", Merry.Salary);
            Console.WriteLine("Annual Package = {0}", MerryAnnualSal);
            Console.WriteLine("Pending Leaves = {0}", MerryPendingLeaves);
 
            Console.WriteLine("\n** Ramesh Details**");
            Console.WriteLine("Name = {0}", Ramesh.Name);
            Console.WriteLine("Birth Date = {0}", Ramesh.BirthDate);
            Console.WriteLine("Monthly Salary = {0}", Ramesh.Salary);
            Console.WriteLine("Annual Package = {0}", RameshAnnualSal);
            Console.WriteLine("Pending Leaves = {0}", RameshPendingLeaves);
        }
 
        static void Main(string[] args)
        {
           //created object of program class
            Program prog = new Program();
            prog.computeAnnulSalAndPendingLeaves();
            Console.ReadLine();
        }
    }

Below is output for the program

Class in C#














This is one simple example of using class. There are lots of classes that are available in dot net framework that we can use to do various things. Lets say for example we have sqlConnection class there to connect to sql database through our dot net code.
see the below example where i used the sqlConnection class from dot net framework.

+
 
SqlConnection con = new SqlConnection();  
con.ConnectionString = "your connection string to connect to a database"; 

No comments :