Modern programming has some relation to real-world applications and scenarios. Also, there are various cases and points that programmers need to consider for developing an application. OOP (Object Oriented Programming) brings some of the most precious and useful concepts along with it. One of them is the concept of encapsulation that is done using classes. Classes and objects revolve around real-life entities. In this chapter, you will learn about the concept of classes and objects and how they are used and beneficial.

What Are Classes in C#?

Classes are user-defined blueprints or prototype that is used to define real-world entities through objects. They bind data and its associated functions or operations into a single unit. They consist of fields and methods (member functions defining specific operations or working) with data members' help, i.e., variables. This is how classes help in encapsulating the data. Moreover, C# classes support the concept of data binding, polymorphism, inheritance using the help of child class, which is also known as a derived class, and parent class, which are also known as a base class.

Declaring a Class in C#

A class can be declared using the keyword class, followed by the identifier, which is the class's name. Sometimes the modifier is used as a complement to the class-name, and lastly, the body of the class is enclosed by a curly brace. Also, an optional base class or superclass is used to declare a derived data type.

The elements of the class declaration are:

  • Access modifiers: The modifier is used at the very beginning of the class declaration. It can be public, private, internal, or protected.
  • Keyword class: The class keyword is used for declaring the type class.
  • Class name: Here, the name of the class is provided, which is a valid C# identifier, and it should start with an initial letter that that goes with the variable naming convention.
  • Base class or parent class: A parent class is also added (if any), preceded by the : (colon) for inheritance. This is an optional one and is used in the case of inheritance only.
  • Body: The body of the class is defined after the syntax mentioned above is written. A pair of curly braces enclose the class body.
Example Program:
using System;
public class ClassEg
    public int g, h;
    public void disp()
        Console.WriteLine("Example of class in C#.");

Objects in C#

The primary objective of OOPs is to represent all real-world entities. That is where objects come into the picture because they are the instance of a class. They contain a valid address in the memory as well as occupy some space. The main characteristics of objects are they can communicate with the class members and also with each other without knowing the details of code or data associated with it.

Declaring Objects

Objects in C# are declared using the new keyword, which creates a new memory location for storing the object.


<class-name> <object-name> = new <class-name> (parameter—list);


using System;
public class Employee {
 public int Empid;
 public String Empname;

class TestEmp {
 public static void Main(string[] args) {
  Employee e1 = new Employee();
  e1.Empid = 101;
  e1.Empname = "Alex Ray";

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram