Types Of Classes in C#

In C#, there are several types of classes:

Concrete Classes

These are ordinary classes with their own methods and properties that can be instantiated. For instance:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Salary { get; set; }

    public void PrintInformation()
    {
        Console.WriteLine("Id: {0}, Name: {1}, Salary: {2}", Id, Name, Salary);
    }
}

Abstract Classes

These classes are designed to be inherited by other classes and cannot be instantiated. Methods without an implementation that must be overridden by the derived class can be found in abstract classes. For instance:

abstract public class Shape
{
    public abstract double Area();
}

Interface Classes

A class must implement the methods and attributes that are defined by these classes. Interfaces just define the method signature; they do not offer a method implementation. For instance:

interface IPrintable
{
    void Print();
}

Sealed Classes

No other class may inherit from these classes. In order to avoid inheritance and class overriding, a sealed class is utilized. For instance:

sealed public class SecureData
{
    public string Encrypt(string data)
    {
        // Encryption logic here
    }
}

Static Classes

These classes have static members and cannot be instantiated. Methods and properties that do not require access to the class’ non-static members are stored in static classes. For instance:

static public class MathHelper
{
    public static double Pi = 3.14;
    public static double CircleArea(double radius)
    {
        return Pi * radius * radius;
    }
}

These are some of the most popular class types in C#, each with unique properties and applications.

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories