c sharp

C# Classes

C# is OOP (object-oriented Programming) language. OOP is a model or style that deals with classes and objects. There are multiple advantages of Object Oriented programming language, it makes the code look neat and it increases the readability and reusability of the code. It provides security by hiding sensitive data from other users. The biggest benefit of Object Oriented Programming is security because all the data should not be open to all the users. There are different concepts in OOP to make data security possible like inheritance, encapsulation, polymorphism, and abstraction. Class is like a draught; it contains different data types called data members and different functions called member functions. These functions and data members belong to that class. We make an instance of that class termed as an object to access any data member or member function of that specific class. One class can have multiple objects to access data member or member functions.


class Name_of_Class


The ‘class’ word is used to define the class. ‘Name_of_Class’ could be any name we want to give to the class. We will give ‘employee’ as the class name if we create a class to maintain the record of different employees of the organization. Then open the curly braces, inside these braces we can define data members and member functions.

Types of Classes:

  • Public Class: Other classes will easily access the data of the Public class. The data in this class is visible to other classes. If the data is private and should not be visible to other classes, then this public is not preferred to use.
  • Private Class: Its data members and functions are kept private. It means the data is only accessible and visible in this class. A private class is created when data is sensitive and not open to everyone.
  • Protected Class: Its data members and functions are accessible within any other classes that are derived from the protected class.
  • Internal Class: Its data members and member methods are accessible in the assembly. Its data members cannot be accessible outside the assembly.
  • Protected Internal Class: The data of the protected internal class can be accessible inside the assembly and can be accessible by the classes that are derived from this assembly.

Example 1:

This code will elaborate on the use of classes and call the object of the class.

In the first example code, we have defined first a class of ‘animals_no’. Here ‘animals_no’ contains the name of the class by which we will create objects or we can say instance of the class to access the data members and member methods. Then, open the curly braces. Inside these curly braces, we will define and initialize the variable of different data types. The classes can be of different types i.e., public, protected, private, or internal, etc. Within the class, we can declare data members of different types.

In this program, we defined the data type of the variables and initialized them. We can set the accessibility of functions and data members. We set their accessibility according to our needs. Three data members of the class are defined in this program having integer type and the class name is ‘animals_no’. Hence, we will get the total number of animals in the class. After initializing the data members, we will now create the objects of the class ‘animals_no’. The objects are created inside the Main() method. Every program has a Main() method in which we call other member functions and create objects of classes. Here, we construct the object of class, i.e., animals_no animals= new animals_no(). This way, we create the instance of classes. Here, ‘animals_no’ is the name of the class and ‘animals’ is the instance name. Because we wanted to get the total number of animals in the class, we initialize a variable ‘total’ of integer type. Add all three data members. When we want to access any member of a class we call it: object_name.data_member_name i.e., animals.dog. The ‘animals’ shows the object name and the ‘dog’ shows the data member name. Now, display a text on the console by calling the Console.WriteLine() function. To display the total animals in that class, pass the variable ‘total’ as the argument to Console.WriteLine() function. Finally,, utilize the Console.ReadKey(); statement.

Example 2:

Now, we observe how we define and declare the data functions inside the class.

After using the namespace program2, declare a class and assign this class a name. Here it is named ‘area_of_rectangle’. Within this class, define and initialize two variables to calculate the area of the rectangle. One variable is for storing the length and the other is for storing the width. Then, set the accessibility of these data members to private. It indicates that only within this class these variables be accessible. The variables have an integer data type. Inside the class, define a function named ‘formula()’. This method also belongs to an integer data type. Set the function formula() as public because we will access this method to show the result. In the formula() method, perform multiplication operation on the two specified variables ‘length’ and ‘width’. This will return the result after multiplying the values of these variables.

In the main() function, create an object to access the data of class ‘area_of_rectangle’. Create an object in this way i.e., ‘area_of_rectangle’ area=new ‘area_of_rectangle’(). Here, the ‘area’ shows the name of the object. Then, print the message on the terminal by using the Console.WriteLine() function and concatenate the area.formula() in it. The ‘area.formula()’ will call the value of the formula() method and represent the result.


This guide explains the types, uses, the necessity of classes, and how classes can be created in C# language. The classes make things easy and readable. Classes provide data security and we also discussed the accessibility and syntax of classes. The concept of OOP (Object Oriented Programming) provides tons of benefits and in every programming language, the OOP model makes programming easy and understandable. The data functions and objects of the class can be accessed multiple times.

About the author

Ann-ul Hayyat

I'm a professional SEO and technical content writer with extensive experience in these fields as well as writing content for blogs, websites, reports, and other topics related to education, science, sports, and many other areas. I am skilled and experienced in creating SEO-friendly blog posts, technical articles, and other content on themes that you may find interesting.