What Are The Types of Classes in C#?

Introduction

One essential component of object-oriented programming (OOP) in C# is the class. They are used to specify object blueprints, which encapsulate information and behavior. In C#, there are different kinds of classes, each with a distinct function. Let's examine a few of these kinds using some examples:

  • Regular Class
  • Abstract Class
  • Sealed Class
  • Static Class

Let's discuss each one in detail:

Regular Class

An essential component of object-oriented programming (OOP) in C# is a regular class. It is a model or blueprint that outlines an object's composition and actions. By encapsulating data (in the form of fields and properties) and behavior (in the form of methods), regular classes are used to create objects.

public class Car
{
    // Fields
    public string Model;
    public int Year;

    // Properties
    public string Color { get; set; }

    // Method
    public void StartEngine()
    {
        Console.WriteLine("Engine started!");
    }
}

Abstract Class

The purpose of abstract classes is to serve as the foundation for other classes. They may contain abstract methods that must be implemented by derived classes and are not self-instantiating.

public abstract class Shape
{
    // Abstract method
    public abstract double CalculateArea();
}

public class Circle : Shape
{
    public double Radius { get; set; }

    public override double CalculateArea()
    {
        return Math.PI * Math.Pow(Radius, 2);
    }
}

Sealed Class

Sealed classes are not transferable. They serve as a barrier against additional class expansion or modification.

public sealed class FinalClass
{
    // Class implementation
}

Static Class

All members of static classes must be static, and they cannot be instantiated. When methods in utility classes can be called without first creating an instance of the class, they are frequently utilized.

public static class MathHelper
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

Conclusion

Understanding the distinct classifications of C# classes is necessary for competent object-oriented programming. While sealed classes forbid further extension, regular classes give objects a fundamental structure, abstract classes permit abstraction and specialization, and static classes provide functionality without instantiation. Comprehending the distinct classifications of C# classes is imperative for proficient object-oriented programming. While sealed classes forbid further extension, regular classes give objects a fundamental structure, abstract classes permit abstraction and specialization, and static classes provide functionality without instantiation.


Similar Articles