Back to: C#.NET Tutorials For Beginners and Professionals
Class and Objects in C# with Examples
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 basic 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.
- Class and Objects from Layman’s Point of View.
- Class and Objects from Programming Language Point of View.
- How can we create a Class and Object in C#?
- Difference between Class and Objects in C#
- 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 of view as well as from the programming point of view.
Class and Objects from Layman’s Point of View.
Most people will say that everything that you can see and touch in the world is an object and there is a class for it. So let us understand what it means. Classes come from classification. If you take anything in the world you can say that this belongs to so-and-so class. If you take a group of students at a college then you can say that these students belong to computer science, and this is a class of computer science and these students belong to electronics and this is the class of electronics.
So, here, we are classifying based on the subject or the course they are going through. It means that classification is based on the criteria that we are adopting.
So, classification is done based on some criteria or the common things that you can find in them. We define classes in our daily life. Any 4-wheeler vehicle in which there is a driver and some people are sitting at the back or beside the driver, then we say it’s a car. So, this is the classification depending on the property we are defining. So, based on the properties we can define whether it is a truck or it is a car and both are vehicles again, so the vehicle is again a class.
If we take examples of human beings, it is a class. There’s a class human and you are an object of human being class. The BMW is a car and Toyota is also a car. These are the objects of class cars. So, class is a definition and objects are instances.
Some companies provide housing facilities for their employees. They will have some cargo or apartments or independent houses. All the apartments will be similar because they follow the same design. So, design is one and there are the houses or the apartments or the flats that are based on the design. This design is nothing but class and the houses created with the help of that design are objects.
Design is nothing but the blueprint of the house object. So, the house will be having all those things that are defined in the design plan or in the blueprint.
So, every engineer does some paperwork or designing work, and based on that design the manufacturing is done. When you make a product, that product will be having all those things that are there in the design and you can then make many products based on that design.
A car company will design a new car. Then they will manufacture many cars based on that design. So, all those cars are objects and the design that the company is holding with them is a class. In the same way, we also want to write a class and create objects in our program.
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 is 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.
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.
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.
As you can see in the above image, here we have one class called “Employee”. All the Employees have some properties such as employee id, name, salary, gender, 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 the 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 Emp1 and Emp2, then both Emp1 and Emp2 are Employees, so they can be classified as belonging to the Employee class. Both have the same methods (InsertData and DisplayData) but are different in models (properties or fields).
Types of Classes in C#:
Please have a look at the following image.
In C# we have the below types of classes
- Abstract Class
- Concrete class
- Sealed Class
- Partial Class
- 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 this Class and Objects in C# article. In the next article, I am going to discuss Constructors in C# and their types in detail with Examples.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.