C# Constructors Explained: Types, Chaining and Best Practices
Constructors are special members of a C# class that are executed when an object is created. They are commonly used to initialize fields, validate input, and place an object into a valid starting state.
What is a Constructor?
A constructor has the same name as its class and does not have a return type. It runs automatically when an instance of the class is created with new.
class User
{
public User()
{
Console.WriteLine("User object created");
}
}
User user = new User();
Default Constructor
A parameterless constructor does not require arguments. If a class has no instance constructors declared, C# can provide a default parameterless constructor automatically.
class Product
{
public Product()
{
Console.WriteLine("Product initialized");
}
}
Parameterized Constructor
A parameterized constructor accepts values during object creation and can use them to initialize object properties or fields.
class Product
{
public string Name { get; set; }
public double Price { get; set; }
public Product(string name, double price)
{
Name = name;
Price = price;
}
}
Product product = new Product("Laptop", 75000);
Constructor Overloading
A class can have multiple constructors with different parameter lists. This allows objects to be created in different ways.
class User
{
public string Name { get; set; }
public int Age { get; set; }
public User()
{
Name = "Unknown";
}
public User(string name)
{
Name = name;
}
public User(string name, int age)
{
Name = name;
Age = age;
}
}
Constructor Chaining with this
The this keyword can be used to call another constructor in the same class. Constructor chaining helps reduce repeated initialization code.
class User
{
public string Name { get; set; }
public int Age { get; set; }
public User() : this("Unknown", 0)
{
}
public User(string name) : this(name, 0)
{
}
public User(string name, int age)
{
Name = name;
Age = age;
}
}
Calling a Base Class Constructor
The base keyword can be used to invoke a constructor from the parent class when creating an object of a derived class.
class Animal
{
public string Name { get; }
public Animal(string name)
{
Name = name;
}
}
class Dog : Animal
{
public Dog(string name) : base(name)
{
}
}
Static Constructor
A static constructor initializes static members of a type. It does not accept parameters and cannot be called directly.
class Configuration
{
public static string AppName { get; private set; }
static Configuration()
{
AppName = "My Application";
}
}
Constructor Types Comparison
| Constructor Type | Purpose | Parameters |
|---|---|---|
| Parameterless | Create an object without input | None |
| Parameterized | Initialize using supplied values | One or more |
| Overloaded | Provide multiple creation options | Varies |
| Static | Initialize static members | None |
| Base Constructor | Initialize inherited state | Depends on base class |
Using Constructors for Validation
Constructors can validate required values before an object is created. This helps prevent invalid object states.
class BankAccount
{
public string AccountNumber { get; }
public BankAccount(string accountNumber)
{
if (string.IsNullOrWhiteSpace(accountNumber))
{
throw new ArgumentException("Account number is required.");
}
AccountNumber = accountNumber;
}
}
Constructors and Object Initialization
Constructors and object initializers can work together. Constructors are useful for required initialization, while object initializers provide convenient property assignment.
class Customer
{
public string Name { get; set; }
public string City { get; set; }
public Customer(string name)
{
Name = name;
}
}
Customer customer = new Customer("Alice")
{
City = "Bengaluru"
};
Common Constructor Mistakes
- Putting too much business logic inside constructors
- Allowing invalid objects to be created
- Duplicating initialization logic across overloaded constructors
- Forgetting to initialize required members
- Creating constructors with too many parameters
Constructor Best Practices
- Keep constructors focused on initialization
- Validate required arguments
- Use constructor chaining to reduce duplication
- Make object state valid after construction
- Use dependency injection for external dependencies
- Avoid expensive operations unless they are truly required
Real-World Applications
- Initializing user objects
- Creating database models
- Configuring services
- Validating domain objects
- Initializing application settings
- Passing dependencies into services
Practice Exercises
- Create a Student class with a parameterized constructor
- Build overloaded constructors for a Product class
- Use this for constructor chaining
- Create a base class and derived class constructor
- Validate constructor arguments
- Create a class with a static constructor
Conclusion
Constructors are fundamental to object-oriented programming in C#. They provide a reliable way to initialize objects, validate required data, and establish a valid starting state. Understanding constructor overloading, chaining, inheritance, and static initialization helps developers design cleaner classes.