Class and Objects in C#

Class and Objects in C#

In this article, I am going to discuss Class and Objects in C# with examples. Please read our previous article before proceeding to this article where we discussed the basics concepts of Object-Oriented Programming. Understanding class and objects in C# is very important for you as a developer. This is because everything in C# is in the form of class and objects. As part of this article, we are going to discuss the following pointers in detail.

  1. Class and Objects from Layman Point of View.
  2. Class and Objects from Programming Language Point of View.
  3. How can we create a Class and Object in C#?
  4. Difference between Class and Objects in C#
  5. Types of classes in C#
Class and Objects in C#

As we already discussed in our previous article, class, and objects addresses the reusability functionality. Again we discussed in Object-Oriented Programming, we need to think in terms of objects rather than functions. So, let us discuss what exactly classes and objects are from the Layman point view as well as from the programming point of view.

Class and Objects from Layman Point of View.

From the layman point of view, we can define a class as a blueprint of a specific object. Every living and non-living thing is considered as objects such as Car, People, Place, etc. Again, each and every object has some color, shape, properties, and functionalities.

For example, consider the luxury car Ferrari. Here, Ferrari is an object of the luxury car type. The luxury car is a class that specifies some characteristics such as speed, color, shape, etc. So any car manufactures company that makes a car and if that car meets all those requirements, then it is an object of the luxury car type.

If you take the example BMW, Lamborghini, and Cadillac, then all these cars are an object of the ‘Luxury Car’ class. Here, ‘Luxury Car’ is a class and every single car (BMW, Lamborghini, and Cadillac) is an object of the luxury car class. Now, let us understand what exactly class and objects from the programming point of view.

Class and Objects from Programming Language Point of View.

Here we are going to understand the class and objects from the C# programming language point of view. But this is also applicable to any object-oriented programming language like java and c++.

Class:

A class is simply a user-defined data type that represents both state and behavior. The state represents the properties and behavior is the action that objects can perform.

In other words, we can say that a class is the blueprint/plan/template that describes the details of an object. A class is a blueprint from which the individual objects are created. In C#, a Class is composed of three things i.e. a name, attributes, and operations.

Objects:

It is an instance of a class. A class is brought live by creating objects. An object can be considered as a thing that can perform activities. The set of activities that the object performs defines the object’s behavior.

All the members of a class can be accessed through the object. To access the class members, we need to use the dot (.) operator. The dot operator links the name of an object with the name of a member of a class.

How can we create a Class and Object in C#?

Let us understand how to create class and object in C#. In order to understand this, please have a look at the following image. As you can see in the below image, a class definition starts with the keyword class followed by the class name (here the class name is Calculator), and the class body enclosed by a pair of curly braces. As part of the class body, you define class members (properties, methods, variables, etc.). Here as part of the body, we define one method called CalculateSum. The class Calculator is just a template. In order to use this class or template, you need an object. As you can see in the second part of the image, we create an object of the class Calculator using the new keyword. And then store the object reference on the variable calObject which is of type Calculator. Now, using this calObject object we can access the class members using a dot.

How can we create a Class and Object in C#

So, the point that you need to remember is, to create a class you need to use the class keyword while if you want to create an object of a class then you need to use the new keyword. Once you create the object then you can access the class members using the object.

The complete example code is given below.
using System;
namespace ClassObjectsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating object
            Calculator calObject = new Calculator();

            //Accessing Calculator class member using Calculator class object
            int result = calObject.CalculateSum(10, 20);

            Console.WriteLine(result);
            Console.ReadKey();
        }
    }

    //Defining class or blueprint or template
    public class Calculator
    {
        public int CalculateSum(int no1, int no2)
        {
            return no1 + no2;
        }
    }
}

Output: 30

Difference between Class and Objects in C#

Many programmers or developers still get confused by the difference between class and object. As we already discussed, in object-oriented programming, a Class is a template or blueprint for creating Objects and every Object in C# must belong to a Class. Please have a look at the following diagram to understand the difference between them.

Difference between Class and Objects in C#

As you can see in the above image, here we have one class called “Employee”. All the Employees having some properties such as employee id, name, salary, gender, and department, etc. These properties are nothing but the attributes (properties or fields) of the Employee class.

If required you can also add some methods (functions) that are common to all Employees such as InsertData and DisplayData to insert and display the Employee Data.

So, the idea is that the template or blueprint of Employee is not going to change. Each and every Object is going to build from the same template (Class) and therefore contains the same set of methods and properties.

Here, all Objects share the same template but maintain a separate copy of the member data (Properties or fields).

For example: if we create two employees, let’s say e1 and e2, then both e1 and e2 are Employees, so they can be classified as belonging to the Employee class. Both have the same methods (InsertData and DisplayData) but different in models (properties or fields)

Types of classes in C#:

Please have a look at the following image.

Types of Classes in C#

In C# we have below types of classes

  1. Abstract class
  2. Concrete class
  3. Sealed class
  4. Partial Class
  5. Static class

We will discuss each of these classes in detail in our upcoming articles.

Here, in this article, I try to explain Class and Objects in C#. I hope you understood the class and objects in C#. In the next article, I am going to discuss Constructors in C# and its types in details with examples.

Leave a Reply

Your email address will not be published. Required fields are marked *