Constructors in C#
Discover how to initialize objects using constructors in C#.
Constructors in C# is a free C# Academy lesson on CoddyKit — lesson 4 of 7. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 7 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Constructors in C#
Welcome to the next lesson! In this lesson, you’ll learn about constructors in C#, their purpose, and how to use them to initialize objects. Let’s get started!

2
What Is a Constructor?
A constructor is a special method in a class that is automatically called when an object of the class is created. It is used to initialize the object’s data or perform setup tasks.
Key Point: A constructor has the same name as the class and no return type.
3
Defining a Constructor
To define a constructor, create a method with the same name as the class. Example:
Tip: Use constructors to ensure your objects are always created in a valid state.
using System;
class Car {
public string Color;
// Constructor
public Car() {
Color = "Red"; // Initialize the Color attribute
}
}
class Program {
static void Main() {
Car myCar = new Car(); // Constructor is automatically called
Console.WriteLine("Car color: " + myCar.Color); // Outputs: Car color: Red
}
}
4
Parameterized Constructors
A parameterized constructor allows you to pass values to initialize object data when creating the object. Example:
Key Point: Use parameterized constructors for flexible object initialization.
using System;
class Car {
public string Color;
// Parameterized Constructor
public Car(string color) {
Color = color; // Initialize Color with the passed value
}
}
class Program {
static void Main() {
Car myCar = new Car("Blue"); // Pass "Blue" to the constructor
Console.WriteLine("Car color: " + myCar.Color); // Outputs: Car color: Blue
}
}
5
Default Constructors
If no constructor is explicitly defined, C# provides a default constructor that initializes fields to their default values. Example:
Tip: Define your own constructor when specific initialization is needed.
using System;
class Car {
public string Color; // Default value is null
}
class Program {
static void Main() {
Car myCar = new Car(); // Default constructor is automatically called
Console.WriteLine("Car color: " + (myCar.Color ?? "Not set")); // Outputs: Car color: Not set
}
}
6
Constructor Overloading
C# supports constructor overloading, which means a class can have multiple constructors with different parameter lists. Example:
Key Point: Constructor overloading provides flexibility when creating objects.
using System;
class Car {
public string Color;
// Default Constructor
public Car() {
Color = "Red";
}
// Parameterized Constructor
public Car(string color) {
Color = color;
}
}
class Program {
static void Main() {
Car defaultCar = new Car(); // Calls the default constructor
Console.WriteLine("Default car color: " + defaultCar.Color); // Outputs: Default car color: Red
Car customCar = new Car("Green"); // Calls the parameterized constructor
Console.WriteLine("Custom car color: " + customCar.Color); // Outputs: Custom car color: Green
}
}
7
Practice Challenge
Create a class called Book with a default constructor that sets the title to "Unknown" and a parameterized constructor that accepts a title. Display the book’s title for both constructors. Example:
using System;
class Book {
public string Title;
// Default Constructor
public Book() {
Title = "Unknown";
}
// Parameterized Constructor
public Book(string title) {
Title = title;
}
}
class Program {
static void Main() {
Book defaultBook = new Book();
Console.WriteLine("Default book title: " + defaultBook.Title); // Outputs: Default book title: Unknown
Book customBook = new Book("C# Programming");
Console.WriteLine("Custom book title: " + customBook.Title); // Outputs: Custom book title: C# Programming
}
}
8
9
Best Practices for Constructors
Here are some best practices when using constructors:
- Keep constructors simple and focused on initialization.
- Avoid complex logic in constructors to ensure clarity.
- Use constructor overloading for flexibility in object creation.
Tip: Constructors should prepare an object to be fully functional immediately after creation.
10
Great Job!
Congratulations! You’ve learned what constructors are, how to define them, and their importance in object initialization. In the next lesson, we’ll explore the concept of inheritance and learn how to reuse code effectively. Let’s keep coding!

Frequently asked questions
Is the “Constructors in C#” lesson free?
Yes — the full text of “Constructors in C#” is free to read here on the web, and the C# Academy course includes 7 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Constructors in C#”?
Discover how to initialize objects using constructors in C#. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 7, so you can start here or from the beginning and move at your own pace.
How long does the “Constructors in C#” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.