What is the difference between classes and objects in C#

Introduction

A class is a blueprint of an entity, and an object is the real value of the entity. In this article, let's learn the relationship and the difference between a class and an object. 

What is a Class?

Class, what comes first to my mind were my school days when my friends and I were put inside a class to learn. Remember the phrase in the grammar that “ Everything in the real world that we can feel, touch or has an identity is an object?" Well, in that class, we have desks, a blackboard, fans, A.C., and obviously the students also. All these identifiable entities are objects.

Now we will see how the classes and objects are related. It might be wrong at this point to say that “ A class is a group of objects”, so if I keep the object in a group, then this group will eventually become a class. So let’s try putting some random object inside a group and create a class out of it. Let’s take out an apple, a shirt, a car, and a mobile and try to create a class out of these objects, Ahhww!!, I cannot create a class as all these objects are different in behavior and have different properties. I can eat an apple, wear a shirt, drive a car, and call on my mobile. All of the objects will have different properties and behaviors. So, let me correct my above statement, “A class is a group of objects that can be uniquely identified and has common properties and behaviors” I think I cleared the air now, so if we put the objects with similar properties and behavior inside a group, then we can create a class out of these objects.

Now let us take an example of the objects that have common properties and behavior, an apple, an orange, a banana, a pineapple, and a mango; all these objects have common properties, just like everyone has a color, a shape, a size, a weight, etc. In addition, they have common behavior like their smell, their taste, etc. So we can group them into a class named Fruits.

I think I have given you a very basic of what a class and object are. Now let us see what classes and objects are in programming. 

Let us take an example of the students. Every student must have a name, date of birth, age, address, father's name, gender, etc. They also have behavior such as reading, writing, studying, talking, etc. So, I can group these properties and behavior of the student and put them all together inside a Student class. Let us see how we can create a Students' class in C#. To create a class in C#, we use the class keyword followed by the name of the class and all the properties and behaviors and placed them inside the class using the curly braces.

The syntax for creating the class is as follows:

class <name of the class>  
{ //opening curly brace  
// Properties of the class   
// Behaviours of the class  
  
}; //closing curly brace, semicolon is optional 

Now, let us create the student class using the above syntax.

class Students {    
 //Properties of the student       
 private string Name;    
 private DateTime Dob;    
 private int Age;    
 private string Address;    
 private string FathersName;    
 private string Gender;    
 //To uniquely identify the object      
 private string RollNo;    
 //Behaviours of the student      
 public void Walking();    
 public void Talking();    
 public void Reading();    
 public void Writing();    
 public void Studying();    
}

A class does not have any physical existence, and it’s a logical group of the properties and behavior of the objects into a single unit. A class is a user-defined reference type data type, or we can say it’s a blueprint or a prototype from which we can create the objects. A class defines a type of object, but it’s not an object. In C#, classes support polymorphism, the inheritance that provides derived classes and base classes, abstraction, and encapsulation.

What is an Object?

An object is an instance of the class and represents a real-life entity. An object is a reference type just like a class, so when we create an object of the class, it contains a null value unit we explicitly initialize it. To initialize an object, we use a new keyword followed by the name of the class that the object will be based on, for example:

To create the object of the student class used above, we can use the below statement.

Students ram;

And to initialize the object, we can write:

ram=new Students();

Or we can combine the above two statements in a single line as below:

Students ram=new Students();

In the above statement, we can see that when an instance of a class is created, a reference of the object is passed back to the object (ram) that is based on the class students, and when the object is created, memory is also allocated to the object on the managed heap for that specific object. The variable (ram) holds only the memory address of the object passed at the time of initializing the object.'

Summary

This article taught us about class and objects in C# and how to use them in programming examples. 

To learn more about classes, objects, and OOPs, follow Object Oriented Programming In C#


Similar Articles