Question
If a class inherits from a base class in C# and you want to pass values from the derived class constructor to the base class constructor, how do you do that?
For example, suppose you inherit from Exception and want to pass the message value to the base Exception constructor:
class MyExceptionClass : Exception
{
public MyExceptionClass(string message, string extraInfo)
{
// How do you pass message to the base Exception constructor?
base(message);
}
}
The goal is to pass the message string to the base Exception class constructor correctly.
Short Answer
By the end of this page, you will understand how constructor chaining works in C#, how to call a base class constructor using : base(...), and why this is especially useful when creating custom exception classes and other derived types.
Concept
In C#, when you create an object from a derived class, the base part of that object must be initialized too. That means the base class constructor runs as part of object creation.
If the base class needs arguments, you must pass them in the constructor declaration, not from inside the constructor body. C# uses constructor initializer syntax for this:
public Child(string value) : base(value)
{
}
This matters because the base class must be constructed before the derived class constructor body runs. By the time execution enters the { ... } body of your derived constructor, base initialization has already happened.
For your Exception example, the correct approach is:
class MyExceptionClass : Exception
{
public string ExtraInfo { get; }
public MyExceptionClass(string message, string extraInfo) : base(message)
{
ExtraInfo = extraInfo;
}
}
This passes message to , while the derived class handles its own additional data.
Mental Model
Think of building a house extension.
- The base class is the original house.
- The derived class is the new extension.
- Before you can finish the extension, the original house must already exist.
So in C#, the base constructor is called first to build the base part of the object. Then the derived constructor finishes the rest.
base(...) is like saying: "First, build the original part using these materials, then I will add my own part."
Syntax and Examples
Core syntax
Use : base(...) after the constructor signature:
public DerivedType(arg1, arg2) : base(baseArg1, baseArg2)
{
// derived class setup
}
Basic example
using System;
class Person
{
public string Name { get; }
public Person(string name)
{
Name = name;
}
}
class Employee : Person
{
public int EmployeeId { get; }
public Employee(string name, int employeeId) : base(name)
{
EmployeeId = employeeId;
}
}
Explanation
Personrequires a .
Step by Step Execution
Consider this example:
using System;
class Person
{
public string Name { get; }
public Person(string name)
{
Console.WriteLine("Person constructor running");
Name = name;
}
}
class Employee : Person
{
public int Id { get; }
public Employee(string name, int id) : base(name)
{
Console.WriteLine("Employee constructor running");
Id = id;
}
}
var employee = new Employee("Ava", 42);
Console.WriteLine(employee.Name);
Console.WriteLine(employee.Id);
What happens step by step
new Employee("Ava", 42)starts creating anEmployeeobject.- C# sees
: base(name)in theEmployeeconstructor.
Real World Use Cases
Using base constructors appears in many practical C# scenarios:
Custom exceptions
class ValidationException : Exception
{
public ValidationException(string message) : base(message)
{
}
}
Used when you want standard exception behavior plus your own exception type.
Reusing common initialization
class DbEntity
{
public Guid Id { get; }
public DbEntity(Guid id)
{
Id = id;
}
}
class User : DbEntity
{
public string Email { get; }
public User(Guid id, string email) : base(id)
{
Email = email;
}
}
Useful when multiple classes share common fields.
UI or framework classes
Some framework base classes need configuration values during construction. Derived classes pass those values upward.
Real Codebase Usage
In real projects, developers commonly use base constructor calls in these patterns:
Passing required state upward
If the base class owns a piece of data, let the base class initialize it.
class AppError : Exception
{
public AppError(string message) : base(message) { }
}
Keeping derived constructors small
Avoid repeating base setup logic in every child class.
class Entity
{
public DateTime CreatedAt { get; }
public Entity()
{
CreatedAt = DateTime.UtcNow;
}
}
class Order : Entity
{
public decimal Total { get; }
public Order(decimal total) : base()
{
Total = total;
}
}
Validation before storing local values
You can still validate arguments in the derived constructor body, but constructor chaining happens before the body runs. If the validation affects values passed to the base constructor, do it in the argument expression.
Common Mistakes
1. Trying to call base(...) inside the constructor body
This is the mistake from the original question.
Incorrect
class MyExceptionClass : Exception
{
public MyExceptionClass(string message)
{
base(message); // Invalid
}
}
Correct
class MyExceptionClass : Exception
{
public MyExceptionClass(string message) : base(message)
{
}
}
2. Forgetting that the base constructor runs first
Beginners sometimes expect derived fields to be available before base(...) runs.
class Base
{
public Base(string ) { }
}
:
{
_text;
{
_text = text;
}
}
Comparisons
| Concept | Purpose | Syntax | When to use |
|---|---|---|---|
base(...) | Calls a base class constructor | public Child() : base(...) | When the parent class must be initialized |
this(...) | Calls another constructor in the same class | public Child() : this(...) | When you want to reuse constructor logic within the same class |
base.Member | Accesses a base class method or property | base.ToString() | When you want the parent implementation |
| constructor body | Runs derived class logic after base initialization | { ... } |
Cheat Sheet
class Child : Parent
{
public Child(string value) : base(value)
{
}
}
Rules
- Use
: base(...)after the constructor signature. - The base constructor runs before the derived constructor body.
- You cannot call
base(...)like a normal method inside{}. - If the base class has no parameterless constructor, you must call one of its existing constructors.
- Use
this(...)to call another constructor in the same class.
Exception example
class MyExceptionClass : Exception
{
public MyExceptionClass(string message, string extraInfo) : base(message)
{
ExtraInfo = extraInfo;
}
public string ExtraInfo { get; }
}
FAQ
How do you call a base constructor in C#?
Use : base(arguments) after the derived constructor declaration.
public Child(string name) : base(name)
{
}
Can I call base() inside the constructor body?
No. Base constructors must be called using constructor initializer syntax, not as a statement inside the body.
What happens if I do not call a base constructor explicitly?
C# tries to call the parameterless base constructor automatically. If the base class does not have one, you get a compile-time error.
How do I pass a message to Exception from a custom exception?
Use:
public MyExceptionClass(string message) : base(message)
{
}
What is the difference between base(...) and this(...)?
base(...)calls a constructor in the parent class.
Mini Project
Description
Create a small C# example with a base class and a derived class that passes data to the base constructor. This project helps you practice constructor chaining in a realistic way by modeling an application-specific exception with extra context.
Goal
Build a custom exception class that passes its message to Exception and stores additional information in its own property.
Requirements
- Create a class that inherits from
Exception. - Add a constructor that accepts
messageand one extra value such asextraInfo. - Pass
messageto the baseExceptionconstructor using: base(message). - Store the extra value in a public read-only property.
- Throw the exception and print both the standard message and the extra information.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
C# Type Checking Explained: typeof vs GetType() vs is
Learn when to use typeof, GetType(), and is in C#. Understand exact type checks, inheritance, and safe type testing clearly.
C# Version Numbers Explained: C# vs .NET Framework and Why “C# 3.5” Is Incorrect
Learn the correct C# version numbers, how they map to .NET releases, and why terms like C# 3.5 are inaccurate and confusing.