Class Types — C#

Classes are data structures in object-oriented programming that define how objects will behave and what they can do. There are various types of classes, each serving a specific purpose. Understanding each type's purpose and characteristics allows for selecting the most appropriate one for specific scenarios, resulting in cleaner and more maintainable code.
Class Types
- Regular/Concrete Class
- Abstract Class
- Static Class
- Sealed Class
- Partial Class
- Generic Class
1. Regular/Concrete Class
Concrete classes are basic classes created without any special keywords or modifiers. These are the fundamental building blocks of object-oriented programming and are commonly used as starting points. We use concrete classes to create instances with the new keyword.
// Regular Class Example
csharp
public class Person
{
// Properties
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
// Constructor
public Person(string firstName, string lastName, int age)
{
FirstName = firstName;
LastName = lastName;
Age = age;
}
// Method
public void Promotion()
{
Console.WriteLine($"Hello, my name is {FirstName} {LastName} and I am {Age} years old.");
}
}
Usage
When defining the fundamental characteristics and behaviors of an object.
For classes that don’t require special modifications.
When creating classes that can be inherited and extended by others.
2. Abstract Class
Abstract classes cannot be instantiated directly; they are designed to be inherited by other classes. Abstract classes define abstract members that derived classes must implement. They can also contain non-abstract members that provide basic functionality. Abstract classes are created using the abstract keyword and cannot be instantiated directly.
// Abstract Class Example
csharp
public abstract class Shape
{
public abstract double CalculateArea(); // Abstract method, must be implemented by derived classes.
public void DisplayInfo()
{
Console.WriteLine("This is a shape.");
}
}
public class Circle : Shape
{
public double Radius { get; set; }
public Circle(double radius)
{
Radius = radius;
}
public override double CalculateArea()
{
return Math.PI * Radius * Radius;
}
}
public class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }
public Rectangle(double width, double height)
{
Width = width;
Height = height;
}
public override double CalculateArea()
{
return Width * Height;
}
}
Usage
When creating a template for future classes to follow.
For providing a common base for related class groups.
To define default methods that derived classes can override.
3. Static Class
Static classes can only contain static members and cannot be instantiated. They are typically used for utility or general-purpose functionality that doesn’t rely on object instances. Static classes are defined with the static keyword and only allow static fields, properties, and methods.
// Static Class Example
csharp
public static class MathUtilities
{
public static double Pi = 3.14159;
public static double Multiply(double a, double b)
{
return a * b;
}
}
class Program
{
static void Main(string[] args)
{
double product = MathUtilities.Multiply(2, 8);
Console.WriteLine($"Product: {product}");
}
}
Usage
When creating helper methods that should be called without object instances.
For general-purpose functions that don’t depend on instance data.
4. Sealed Class
Sealed classes cannot be inherited by other classes. The sealed keyword indicates that a class should not be extended, often for security, performance, or integrity reasons. Sealed classes provide a layer of control over the class hierarchy, ensuring other developers can’t extend or modify them.
// Sealed Class Example
csharp
public sealed class CommonCalculator
{
public int GetTotal(int x, int y)
{
return x + y;
}
}
Usage
When creating critical components that shouldn’t be extended.
To improve performance by ensuring no further inheritance.
5. Partial Class
Partial classes allow splitting a class definition across multiple files, which can help organize code in larger projects or enable multiple developers to work on different parts of a class simultaneously. Partial classes are defined using the partial keyword.
// Partial Class Example
csharp
// File: CustomUrl.Customer.cs
public partial class CustomUrl
{
public string GetCustomerProductUrl()
{
return "/api/getcustomerproduct/";
}
}
// File: CustomUrl.Auth.cs
public partial class CustomUrl
{
public string GetTokenUrl()
{
return "/api/gettoken";
}
}
Usage
When using code generation tools that modify the class, separating generated code from custom code.
For large classes where organizing code across multiple files improves readability.
When multiple developers need to work on different parts of a single class simultaneously.
6. Generic Class
Generic classes can work with any data type, reducing code duplication and improving type safety. They are defined with type parameters (usually
// Generic Class Example
csharp
public class GenericClass
{
private T data;
public GenericClass(T value)
{
data = value;
}
public T GetData()
{
return data;
}
public void DisplayData()
{
Console.WriteLine("Data: " + data);
}
}
Usage
For creating reusable code that works with multiple data types.
To reduce code duplication and simplify maintenance.
To enhance type safety and catch errors at compile-time.