1. Regular Class
Overview
- A class is a reference type that represents an object with properties, fields, and methods.
- It allows both mutability (ability to change object state) and encapsulation (hiding internal state).
Key Features
- Supports inheritance and polymorphism.
- Encapsulates behavior using methods and fields.
- State can change over time (mutable).
- Two objects of the same class with identical values are not equal unless you override the equality members (like
Equals
andGetHashCode
).
Example of a Class:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public override string ToString() => $"Name: {Name}, Age: {Age}";
}
C#When to Use?
- When the focus is on behavior along with data.
- When you need inheritance and polymorphism.
- When the object’s state will change frequently.
2. Static Class
A static class is a class that cannot be instantiated. It contains only static members (fields, methods, etc.).
Features
- Cannot be instantiated, inherited, or contain instance members.
- Useful for utility functions or shared constants.
- Memory-efficient, as there’s no need to create an object.
Example:
public static class MathUtilities
{
public static int Add(int a, int b) => a + b;
}
C#When to Use:
- For utility functions or global methods (e.g., Math operations).
- When the state does not need to be stored across instances.
When Not to Use:
- Avoid when you need object-level state or inheritance.
- Use regular or abstract classes for behavior-driven objects.
3. Abstract Class
An abstract class serves as a base class that cannot be instantiated directly. It defines a template for derived classes by providing abstract or non-abstract members.
Features
- Can contain both abstract (no implementation) and non-abstract methods.
- Requires inheriting classes to implement abstract members.
- Useful for common behavior between derived classes.
Example:
public abstract class Animal
{
public abstract void Speak();
public void Breathe() => Console.WriteLine("Breathing...");
}
public class Dog : Animal
{
public override void Speak() => Console.WriteLine("Woof!");
}
C#When to Use:
- When multiple classes share a common structure but need different implementations for specific methods.
- Use when you want some members to have default implementations and others to be implemented by derived classes.
When Not to Use:
- If all members need concrete implementations, use regular classes.
- If you don’t need to enforce a base structure, interfaces may be more appropriate.
4. Partial Class
A partial class allows splitting the class definition across multiple files. It is combined into a single class at compile time.
Features
- Useful for large classes or code generated by tools (e.g., UI or EF models).
- All partial files must have the same access level and name.
Example:
file 1
public partial class Employee
{
public string Name { get; set; }
}
C#file 2
public partial class Employee
{
public int Age { get; set; }
}
C#When to Use:
- When a class is too large to fit in a single file for readability.
- In auto-generated code, where manual code coexists with generated code (e.g., designer files in WinForms).
When Not to Use:
- If class size is small and manageable, partial classes may reduce code clarity.
- Avoid splitting without a clear reason—code spread across multiple files can become hard to maintain.
5. Sealed Class
A sealed class prevents other classes from inheriting from it. It is used to restrict inheritance when extending the class might introduce bugs or be unnecessary.
Features
- Prevents inheritance to secure or finalize a class’s behavior.
- Can still be instantiated normally.
Example:
public sealed class ConfigurationManager
{
public string GetConnectionString() => "Server=myServer;Database=myDB;";
}
C#When to Use:
- When you want to prevent further inheritance and ensure the class’s behavior remains unchanged.
- Useful for security-sensitive classes (e.g., cryptography utilities).
When Not to Use:
- If you expect the class to be extended in the future.
- Consider using regular or abstract classes if inheritance is required.
6. Record Class
A record class is designed for immutable data and value-based equality. By default, records treat two objects with the same values as equal.
Features
- Supports value-based equality and deconstruction.
- Immutability by default (though you can make properties mutable).
- Introduced in C# 9.0 for better handling of data-centric models.
Example:
public record Person(string Name, int Age);
var person1 = new Person("Alice", 30);
var person2 = new Person("Alice", 30);
Console.WriteLine(person1 == person2); // True (value-based equality)
C#When to Use:
- When you need immutable data models (e.g., DTOs or state snapshots).
- When you want value-based equality for simpler comparisons.
When Not to Use:
- If the object’s state will change frequently, use a regular class.
- Avoid if you need to maintain complex behavior, as records are best for data-centric structures.
Summary Table
Class Type | Features | When to Use | When Not to Use |
---|---|---|---|
Regular Class | Mutable, supports inheritance and behavior | For behavior-driven objects with mutable state | If you only need static members or immutable data |
Static Class | Cannot be instantiated, only static members | For utility functions and shared logic | If you need instance members or inheritance |
Abstract Class | Cannot be instantiated, requires inheritance | To define a base template for derived classes | If all methods can be implemented directly |
Partial Class | Split class definition across files | For large or auto-generated codebases | For small, manageable classes |
Sealed Class | Prevents further inheritance | To finalize behavior or secure functionality | If future inheritance might be needed |
Record Class | Immutable, value-based equality | For data models or DTOs | If object state changes frequently |
Conclusion
- Regular classes are the most versatile and commonly used.
- Static classes are best for global methods or utility functions.
- Abstract classes help enforce a common structure with optional default behavior.
- Partial classes improve maintainability in large codebases or auto-generated scenarios.
- Sealed classes are useful when you want to lock down the behavior.
- Record classes simplify working with immutable data and value equality. Choose the right class type based on the requirements for mutability, inheritance, or data handling.