When learning C#, understanding the different types of classes is crucial. Classes are the building blocks of object-oriented programming in C#. Here, we’ll explore various types of classes in simple terms, with easy-to-understand examples.

1. Regular Classes

A regular class is a blueprint for creating objects. It can contain fields, properties, methods, and events.

Example:

public class Car
{
    public string Make { get; set; }
    public string Model { get; set; }

    public void DisplayInfo()
    {
        Console.WriteLine($"Car: {Make} {Model}");
    }
}

In this example, Car is a regular class with properties Make and Model, and a method DisplayInfo that prints the car’s information.

2. Static Classes

A static class cannot be instantiated (you can’t create an object from it). All members of a static class must also be static.

Example:

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

    public static int Multiply(int a, int b)
    {
        return a * b;
    }
}

You can call methods in a static class without creating an instance:

int sum = MathOperations.Add(5, 3);
Console.WriteLine(sum); // Outputs: 8

3. Abstract Classes

An abstract class cannot be instantiated directly. It can contain abstract methods (without implementation) and regular methods.

Example:

public abstract class Animal
{
    public abstract void MakeSound();

    public void Sleep()
    {
        Console.WriteLine("Sleeping...");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}

In this example, Animal is an abstract class. The Dog class inherits from Animal and implements the MakeSound method.

4. Sealed Classes

A sealed class cannot be inherited, but it can be instantiated. This means no class can derive from a sealed class, but you can create objects from it.

Example:

public sealed class Calculator
{
    public int Subtract(int a, int b)
    {
        return a - b;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Calculator calculator = new Calculator();
        int result = calculator.Subtract(10, 5);
        Console.WriteLine(result); // Outputs: 5
    }
}

In this example, Calculator is a sealed class. We create an instance of the Calculator class in the Main method and call its Subtract method.

5. Partial Classes

A partial class allows you to split the definition of a class into multiple files. All parts are combined into a single class when the application is compiled.

Example:

// File: Person.Part1.cs
public partial class Person
{
    public string FirstName { get; set; }
}

// File: Person.Part2.cs
public partial class Person
{
    public string LastName { get; set; }

    public void ShowName()
    {
        Console.WriteLine($"{FirstName} {LastName}");
    }
}

When compiled, Person will be a single class with properties FirstName and LastName, and a method ShowName.

6. Nested Classes

A nested class is a class defined within another class. It can access the members of the outer class.

Example:

public class OuterClass
{
    public int OuterValue = 10;

    public class NestedClass
    {
        public void ShowOuterValue()
        {
            OuterClass outer = new OuterClass();
            Console.WriteLine(outer.OuterValue);
        }
    }
}

In this example, NestedClass can access the members of OuterClass.

Summary

  • Regular Classes: Basic blueprints for creating objects.
  • Static Classes: Cannot be instantiated; all members are static.
  • Abstract Classes: Cannot be instantiated; can contain abstract methods.
  • Sealed Classes: Cannot be inherited but can be instantiated.
  • Partial Classes: Split class definitions across multiple files.
  • Nested Classes: Classes defined within other classes.

These different types of classes help organize and manage your code more effectively.

Don’t forget to checkout: C# Basics: Understand the Differences Between Classes, Structs and Records

Categorized in:

.Net,

Last Update: June 20, 2024