What is the difference between classes and objects in C#

Introduction

 
We all have heard of the words class and object, and we all somewhat know about them. Today, I am going to explain in detail classes and objects.
 

What is a Class?

 
Class, what comes first to my mind are my school days, where 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, blackboard, fans, A.C., and obviously the students also. All these identifiable entities are the objects.
 
Now we will see how the classes and objects are related to each other. 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 become eventually a class. So let’s try by 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 object, Ahhww!!, I am not able to 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 make calls on 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. 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 is, 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, a date of birth, an 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 inside the class using the curly braces.
 
The syntax of creating the class is:
  1. class <name of the class>  
  2. //opening curly brace  
  3. // Properties of the class   
  4. // Behaviours of the class  
  5.   
  6. }; //closing curly brace, semicolon is optional 
Now, let us create the student class using the above syntax.
  1. class Students {    
  2.  //Properties of the student       
  3.  private string Name;    
  4.  private DateTime Dob;    
  5.  private int Age;    
  6.  private string Address;    
  7.  private string FathersName;    
  8.  private string Gender;    
  9.  //To uniquely identify the object      
  10.  private string RollNo;    
  11.  //Behaviours of the student      
  12.  public void Walking();    
  13.  public void Talking();    
  14.  public void Reading();    
  15.  public void Writing();    
  16.  public void Studying();    
  17. }  
A class does not have any physical existence, 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. Basically a class defines a type of object, but it’s actually not an object. In C#, classes support polymorphism, the inheritance that provides the concept of 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 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 statement 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, and the variable (ram) hold only the memory address of the object that is passed at the time of initializing the object.'
 

Summary

 
In this article, we learned about class and object in c# and how to use them in programming examples. 
 
To learn more on classes, objects, and OOPs, follow: Object Oriented Programming In C#