In this article I am going to discuss about C# Classes and Objects but before that if you had not read my previous article of C# object-oriented programming concepts then must read it. In previous article we studied 4 pillars or concepts of OOPs. Now we are going to study about C# classes and objects the way to achieve encapsulation in our program.
Introduction to C# Classes and Objects
In C# Encapsulation is the first concept of object-oriented programming which is used to wrapping up of data member and member functions into a single unit. This can be possible with the help of C# Classes and Objects. In C#, classes are a prototype for creating objects and defining their properties and methods. It acts as a template that we can use for creating user-defined objects.
C# Classes
A class is simply a user-defined data type that represents both state and behavior. In other words, we can say that a class is the blueprint or template or prototype that describes the details of an object. A class is a template from which the individual objects are created. In C#, classes can contains data members, member functions and constructors.
C# Objects
It is an instance of a class. An object is known as thing that can perform operations. The set of operations 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. Remember every object must belong to any class.
How to Create Instance of Class?
If you are creating class and want to use the properties and methods of that particular class then you must have to create an instance (object) of a class. In C# we can create the instance of any class using new keyword. The purpose of the new keyword is to create an instance of class so that any change you make to the values only affects to the instance of the class. You can create the object with any name.
//Creating an object of a class
ClassName obj = new ClassName();//I have created object with name of obj
Dot(.) Operator
If you are creating any methods and properties inside any class then you must want to use that properties and methods inside any other class or in main methods. So for using that properties and methods two things are important creating an instance of a class and accessing the methods and properties. In above section we already discussed how we can create instance of class using new operator but only creating instance is not capable to access the methods and properties of class. So in order to access the class’s methods and properties you have to use dot(.) operator with objects. Dot operator is special type of operator that is used to access the methods and properties of any class inside anywhere in the program.
//Accessing the methods of a class Calculator obj = new Calculator(); //There is method in Class Calculator CalculateSum obj.CalculateSum(); //accessing method of class //If your method takes argument then you have to pass it like given below obj.CalculateSum(10,20);
Program to Explain How to Create Classes and Objects in C#?
using System; namespace ClassObjectsDemo { //main class class Program { static void Main(string[] args) { //Creating object Calculator obj = new Calculator(); //Accessing Calculator class member using Calculator class object int result = obj.CalculateSum(10, 20); Console.WriteLine("The sum of two number is : "+result); Console.ReadKey(); } } //Defining class public class Calculator { public int CalculateSum(int no1, int no2) { return no1 + no2; } } }
Output:
The sum of two number is : 30
We have already covered that in a class we can create two types of functions one is static function and other is non-static function. Static functions comes with static keyword and for calling the static function we does not need the object of class we can simply call the static method with class name. But in above program we have created a non-static function. Therefore we have to create one object of class in order to call this function.
Difference between Classes and Objects
Many times most of the beginners confused with the actual difference between classes and objects. So in order to avoid that confusion you can read the following important difference between classes and objects that every programmer must know to understand how objects are different from the classes.
Conclusion
In this C# tutorials article I have discussed about classes and objects in detail. I have also discussed the difference between classes and objects so you can clear with the concept of classes and objects. In my next article I am going to discuss about various type of classes in detail. Hope you found this article helpful and like this article.