Understanding Static, Sealed, and Abstract Classes in C#: A Beginner's Guide with Code Examples

Object-Oriented Programming (OOP) is a programming paradigm that emphasizes the use of classes, objects, and methods to represent real-world concepts and entities in code. In C#, there are three types of classes that are used to implement OOP concepts: static, sealed, and abstract classes. In this article, we'll explain the differences between these three types of classes, how they work in C#, and provide some code examples. 

Static Class 
A static class in C# is a class that is sealed and can only contain static members, such as static fields, static methods, and static properties. You cannot create an instance of a static class. Static classes are often used to provide utility methods or constants that can be accessed throughout an application without the need to create an instance of the class. 

Here is an example of a static class in C#:
public static class Calculator
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
    
    public static int Subtract(int a, int b)
    {
        return a - b;
    }
}
In this example, we have created a static class called Calculator. This class contains two static methods: Add and Subtract. These methods can be accessed anywhere in the application by using the class name followed by the method name, like this:
int sum = Calculator.Add(5, 10);
int sum = Calculator.Subtract(50, 10);
Sealed Class 
A sealed class in C# is a class that cannot be inherited by other classes. Once a class is marked as sealed, it cannot be used as a base class for any other class. Sealed classes are often used to prevent other developers from extending or modifying existing code. 

Here is an example of a sealed class in C#:
public sealed class Rectangle
{
    public int Width { get; set; }
    public int Height { get; set; }

    public int CalculateArea()
    {
        return Width * Height;
    }
}
In this example, we have created a sealed class called Rectangle. This class contains two properties (Width and Height) and a method (CalculateArea). Because the class is sealed, it cannot be inherited by any other class. 

Abstract Class 
An abstract class in C# is a class that cannot be instantiated on its own. Abstract classes are often used to provide a base class that can be inherited by other classes. Abstract classes may contain abstract methods, which are methods that do not have an implementation and must be overridden by any class that inherits from the abstract class. 

Here is an example of an abstract class in C#:
public abstract class Shape
{
    public abstract double GetArea();
    public abstract double GetPerimeter();

    public virtual void PrintDetails()
    {
        Console.WriteLine($"Area: {GetArea()} Perimeter: {GetPerimeter()}");
    }
}

public class Rectangle : Shape
{
    private double _length;
    private double _width;

    public Rectangle(double length, double width)
    {
        _length = length;
        _width = width;
    }

    public override double GetArea()
    {
        return _length * _width;
    }

    public override double GetPerimeter()
    {
        return 2 * (_length + _width);
    }
}

public class Circle : Shape
{
    private double _radius;

    public Circle(double radius)
    {
        _radius = radius;
    }

    public override double GetArea()
    {
        return Math.PI * _radius * _radius;
    }

    public override double GetPerimeter()
    {
        return 2 * Math.PI * _radius;
    }
}
In the above example, the Shape class is declared as abstract, and contains two abstract methods: GetArea() and GetPerimeter(). The Rectangle and Circle classes inherit from the Shape class, and must implement the GetArea() and GetPerimeter() methods. 

In summary, static classes, sealed classes, and abstract classes are three different types of classes in C# with distinct characteristics and use cases. Static classes are used to hold utility methods or constants that do not need to be instantiated. Sealed classes are used to prevent inheritance and modification of class behavior. Abstract classes are used as base classes and contain abstract methods that must be implemented by any derived class. Understanding the differences between these class types is important for writing efficient and effective code in C#. By using these classes correctly, you can make your code more organized, maintainable, and scalable.