Creating Classes and Instances (Objects)
Object-oriented programming (OOP) concept in C# programming language is the process of creating and manipulating multiple class objects from a user defined class data type object creation. In object-oriented programming, the object is the instance feature of the class, where the created class contains class data and various class method parameter arguments. Which performs operations on the created class data by the C# programmer.

So, let’s know the class and instance creation method in C# programming language better.
What is a class in C# programming language?
In C# programming language, we can create a class object and deal with the class object and parameters. In C# programming, class properties define the class data and class methods, which will be present in the object created in the existing class.
Class Define Syntax in C# Programming.
public class ClassName
{
// Create Class Fields (Class data members)
public dataType fieldName;
// We Create Class Constructor
public ClassName()
{
//Let Initialize Class fields or set default values
}
// Define Class Method
public void MethodName()
{
// Write Class Code Nature
}
}
- Class Fields – In C# programming, variables in class program store and process data values for class object.
- Class Constructor – In C# programming, an object of an existing class is constructed and initialized. Constructor is a special class parameter declaration method in a class.
- Class Method – In C# programming language, class functions are methods that define the default behavior of an existing class.
Creating an object (instance) for a class.
In a C# program, an object is an instance element of a class. You can create multiple class object instances in an existing C# program using the new keyword, then these are called in the existing program by the class constructor.
Syntax for creating an object in C# programming.
ClassName objectName = new ClassName();
A class creation example in C# programming.
So let’s start the class program creation step by step with a simple example. Here we define a class named Employee.
using System;
class Program
{
static void Main()
{
// We create an employee class instance
Employee employee1 = new Employee(“Harry”, 27, 9413);
// Let Access properties and lass calling methods
Console.WriteLine(employee1.DisplayInfo()); // result is – Name: John, Age: 25
// We Modify class property values
employee1.Name = “David”;
employee1.Age = 41;
employee1.Contact = 9414;
// It Display employee class updated information
Console.WriteLine(employee1.DisplayInfo()); // result is – Name: Alice, Age: 30
}
}
class Employee
{
// let create class Properties (Fields)
public string Name;
public int Age;
public int Contact;
// we create Constructor to initialize class object
public Employee(string name, int age, int contact)
{
Name = name;
Age = age;
Contact = contact;
}
// It display Method to display employee class information
public string DisplayInfo()
{
return $”\n Employee Name – {Name}, Employee Age – {Age}, Employee Contact – {Contact}”;
}
}
Employee Class Explanation.
Here Employee class has 3 default properties, which are class fields like employee name, employee age, and employee contact.
Employee class has a class constructor employee(string name, int age, int contact) etc. which initializes the employee name, employee age, and employee contact field variables when a new employee class object is created.
Here DisplayInfo class method returns a string containing employee class information.
Here we create a desired class object like Employee employee1 = new Emplyee(“Harry”, 27, 9414). After this we can call the constructor to initialize the employee name, employee age, employee contact properties of the class object.
Constructor Creation Process in C# Programming Language.
A constructor is a special method in a class, which is used to initialize an object, it is called automatically when an object is created.
C# Programming Syntax of Constructor Declaration.
public ClassName(parameters)
{
// let Initialize fields or set default class values
}
In the above class program example.
Here in Employee class (string name, int age, int contact) etc. class is a class constructor object. Which by default accepts three parameter arguments (employee name, employee age, and employee contact) information in Employee class, and assigns them to variable field values of Employee class object.
Remember, in C# program, if no class constructor is defined or declared, then a default class constructor (without class parameters) will be created automatically by C# programming.
C# Program Class Access Modifiers.
Class access modifiers in a C# program define the visibility of class members (class fields, class methods, class constructors, etc.) in the program.
The basic class access modifiers in a C# program are.
- Public – In a C# program class public members can be accessed or called from anywhere in the current class.
- Private – In a C# program class private class members are accessed or called only from within the current class.
- Protected – In a C# program protected class is accessed and processed from within the class and its derived classes.
- Internal – In a C# program class internal class members are called and processed from within the same assembly.
Example of access modifiers in a C# program.
class Employee
{
// Employee class Public property
public string Name;
// Let Create Private field
private int age;
// Here We Create Constructor
public Employee(string name, int age, int contact)
{
Name = name;
this.age = age;
this.contact = contact;
}
// Web Use Public method to access the private field
public int DisplayAge()
{
return age;
}
}
Destructor in C# Programming Language.
In C# programming language, destructor is a class method that is called automatically when a class program object is destructed. In C# programming language, destructor is used only when needed, as it manages the memory in the current program by the garbage collector. Whereas in any C# programming language, a manual class destructor can be created and processed by following the ~ClassName() syntax.
Example of destructor in C# programming language.
class TestClass
{
// Let Create Desire Destructor
~ TestClass ()
{
Console.WriteLine(“\n Destructor called in program, it start object destroyed.”);
}
}
In C# program, the created destructor is called in the current program when a class object is recovered from the garbage in an existing program. Most of the time, software developers do not rely on garbage collection collection to manage the program memory cleanup process, and a programmer or software developer rarely needs to declare and execute a destructor object instance in a program.
Summary of class concept in C# programming language.
- Class in C# – A class in a C# program is a user defined data structure that defines a class element properties and methods.
- Class object – An instance of a class object created in a C# program by using the new keyword in a class.
- Class constructor – A special class method used to initialize a class object in a C# program.
- Class access modifiers – In a C# program, you can define the class object visibility of the class members (public, private, protected, etc.).
- Class static members – Static members in a C# program belong to the class, which are not instances of the existing class.
- Class properties – In a C# program, class private fields provide access to the existing class in a class object.
- Class destructor – It is a class method in C# program which is called in a class when a class object is destructed, destructor method is less used in C# and other programming.