Hello, friends in this tutorial I am going to explain you about Classes and Objects in C#.

Introduction to Classes and Objects:

Classes and Objects are very important topics of Object Oriented Programming. After completing this tutorial you will be able to understand the following topics:

  • Classes in C#.
  • Objects in C#.
  • How to create an object of a class in C#.
  • How to call members of a class.

Class:

A class is a blueprint for an object that encapsulates data and behaviour of a type. The class enables us to create our own custom types by simply grouping events, methods and variable. Classes can be act as templates for other classes in form of inheritance.

Declaring Classes:

A Class can be declared using the class keyword, as shown in the below example:

public class MyClass

{

// Your Data Goes here

}

As you can see in the above example class is preceded by public access modifier. Access modifiers describe who can create an object of this class and who can access this class. We will learn more on Access modifiers in later sessions. After class keyword, we define the name of the class. Inside curly braces, we define properties, methods and events.

As we are already familiar with the fact that classes encapsulate data and method inside itself. Let’s elaborate this fact with few terms related to C#.

  • The Property describes the data that we want to encapsulate inside the class.
  • The Method describes the operation that we want to perform on that data.

To get a better understanding about classes and object let’s move towards real word and understand each term with an example.

Suppose there is a class named Employee. So which type of data the Employee class can hold? EmployeeID, EmploeeName, Address. So the Employee class has the following Properties.

EmployeeID: EmployeeID will be used to store unique number that would represent the Employee.

EmployeeName: This will be used to store Name of the Employee as a String.

EmployeeAddress: This will be used to store Address of the Employee as a String.

Now we know that what data the Employee class will be held. Now we need to come to the second point, what operation we will perform on this data. Our Employee class can have two methods to perform an operation on the data.

SetEmployeeDetails(): This method would be used to set the EmplyeeID, EmployeeName and EmployeeAddress of the Employee.

GetEmployeeDetails(): This method would be used to get the EmployeeID, EmployeeName, EmployeeAddress.

Objects:

An Object in an Object Oriented Programming is a real-time entity. An instance of a class is called the object of that class. We can create as many objects of a class. All objects of a class share the same copy of the member function but maintain a separate copy of properties. We can create an object of a class using “new” keyword. Here is the syntax for creating an object of Employee class.

Employee objEmployee=new Employee();

Note:

  • The “.” operator is used to call member of the class.
  • The “.” Operator links the object name with the member of the class. Suppose Employee class has SetEmployeeDetails method then we can call this method like this.
           objEmloyee. SetEmployeeDetails(param1,param2…).

 

Classes and Objects

Where objEmployee is the name of the object of Employee class. The keyword “new” is responsible for creating an object of Employee class.

Let’s start creating an application for understanding more about Class and Object in C#.

1- Open visual studio and create a console application with the name ClassAndObjectDemo.

Classes and Objects

2-  Create a class with the name Employee.

Classes and Objects

 

Step 3- Declare three variables in Employee Class like this:

       int _iEmployeeID;

       string _sEmployeeName;

       string _sEmployeeAddress;

These variables will store data like EmployeeID, EmployeeName, EmployeeAddress in Employee Class.

Step 4-  Now First thing what we need to do is to store data related to Employee into these variables. For achieving this task we need to create a method that will store data into these variables. Here is the method for storing Employee Data:

  public void SetEmployeeDetails(int iEmployeeID,string sEmployeeName,string sEmployeeAddress)

        {




            _iEmployeeID = iEmployeeID;

            _sEmployeeName = sEmployeeName;

            _sEmployeeAddress = sEmployeeAddress;




        }

The SetEmployeeDetails method accepts three parameter iEmployeeID, sEmployeeName, sEmployeeAddress.

Step 5 – Second thing what we need to achieve is to access data stored in these variables. Here is the method for retrieving Employee Data.

  public void GetEmployeeDetails()

        {

            Console.WriteLine("Employee ID {0}", _iEmployeeID);

            Console.WriteLine("Employee Name {0}", _iEmployeeID);

            Console.WriteLine("Employee Address {0}", _iEmployeeID);

        }

Now our Employee class is ready to execute. That will store and return Employee Data. Here is the complete structure of the Employee Class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;




namespace ClassAndObjectDemo

{

   public class Employee

    {

       int _iEmployeeID;

       string _sEmployeeName;

       string _sEmployeeAddress;

        

        public void SetEmployeeDetails(int iEmployeeID,string sEmployeeName,string sEmployeeAddress)

        {




            _iEmployeeID = iEmployeeID;

            _sEmployeeName = sEmployeeName;

            _sEmployeeAddress = sEmployeeAddress;




        }

        public void GetEmployeeDetails()

        {

            Console.WriteLine("Employee ID {0}", _iEmployeeID);

            Console.WriteLine("Employee Name {0}", _iEmployeeID);

            Console.WriteLine("Employee Address {0}", _iEmployeeID);

        }

       

    }

}

Step 6- Now we need to create an object of the Employee class. Now the question is why we need to create an object of Employee class? Is it not possible to access the members of the Employee class directly? The answer is no. Because we did not mark the Employee class as static. A static class can contain only static members inside the class. We will learn more on static class and static members in later tutorials. Go to Program.cs file and create an object of the Employee class and call the members of the Employee class. Here is the code for Program.cs file.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;




namespace ClassAndObjectDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            Employee objEmployee = new Employee();

            objEmployee.SetEmployeeDetails(1,"Vinod Palne","[email protected]");

            objEmployee.GetEmployeeDetails();

            Console.ReadKey();

         }

    }

}

Run the application and see the output.

Classes and Objects

You can see the Employee Data as an output in console window.

Thank you for spending your precious time. Hope this post about Classes and Objects in C# is useful for you. We will continue on useful topics in later sessions.

Leave a Comment