Defining Constructors
Create instances with parameterized constructors.
Defining Constructors is a free C# Academy lesson on CoddyKit — lesson 1 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Constructor?
A constructor is a special method that runs when an object is created with new. It has the same name as the class and no return type. Its job is to put the object into a valid starting state.
using System;
class Dog
{
public Dog()
{
Console.WriteLine("A dog was created");
}
}
class Program
{
static void Main()
{
var d = new Dog();
}
}Parameterized Constructors
A constructor can take parameters so the caller supplies initial values. This is the most common pattern for setting required data.
using System;
class Person
{
public string Name;
public int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
class Program
{
static void Main()
{
var p = new Person("Ada", 36);
Console.WriteLine(p.Name + " is " + p.Age);
}
}The this Keyword in Constructors
When a parameter name matches a field name, use this. to refer to the field and distinguish it from the parameter.
using System;
class Book
{
public string Title;
public Book(string title)
{
this.Title = title;
}
}
class Program
{
static void Main()
{
var b = new Book("C# Basics");
Console.WriteLine(b.Title);
}
}The Default Constructor
If you write no constructor, the compiler provides a parameterless one. But as soon as you define any constructor, that default is gone unless you add it yourself.
using System;
class Empty
{
// No constructor written -> compiler supplies a default one
public int Value;
}
class Program
{
static void Main()
{
var e = new Empty();
Console.WriteLine("Default Value: " + e.Value);
}
}Validation in a Constructor
A constructor can validate its arguments, guaranteeing the object is never created in an invalid state.
using System;
class Account
{
public decimal Balance;
public Account(decimal initial)
{
if (initial < 0)
throw new ArgumentException("Initial balance cannot be negative");
Balance = initial;
}
}
class Program
{
static void Main()
{
var a = new Account(100m);
Console.WriteLine("Balance: " + a.Balance);
}
}Constructor Overloading
A class can have multiple constructors with different parameter lists. The compiler picks the one matching the arguments you pass.
using System;
class Point
{
public int X;
public int Y;
public Point()
{
X = 0;
Y = 0;
}
public Point(int x, int y)
{
X = x;
Y = y;
}
}
class Program
{
static void Main()
{
var origin = new Point();
var p = new Point(3, 4);
Console.WriteLine(origin.X + "," + origin.Y + " | " + p.X + "," + p.Y);
}
}Setting Properties from a Constructor
Constructors commonly assign to auto-properties, including init-only or private-set ones.
using System;
class Product
{
public string Name { get; }
public decimal Price { get; }
public Product(string name, decimal price)
{
Name = name;
Price = price;
}
}
class Program
{
static void Main()
{
var p = new Product("Pen", 1.50m);
Console.WriteLine(p.Name + ": " + p.Price);
}
}Default Parameter Values
Constructor parameters can have default values, letting callers omit them. This often reduces the need for several overloads.
using System;
class Circle
{
public double Radius;
public string Color;
public Circle(double radius, string color = "black")
{
Radius = radius;
Color = color;
}
}
class Program
{
static void Main()
{
var c1 = new Circle(2);
var c2 = new Circle(3, "red");
Console.WriteLine(c1.Color + " | " + c2.Color);
}
}Doing Work in a Constructor
Besides assigning fields, a constructor can compute derived data or prepare resources the object needs.
using System;
class Rectangle
{
public double Width;
public double Height;
public double Area;
public Rectangle(double w, double h)
{
Width = w;
Height = h;
Area = w * h;
}
}
class Program
{
static void Main()
{
var r = new Rectangle(4, 5);
Console.WriteLine("Area: " + r.Area);
}
}Constructors and Required Data
Putting required values in the constructor makes them mandatory: you cannot create the object without supplying them.
using System;
class User
{
public string Id;
public string Email;
public User(string id, string email)
{
Id = id;
Email = email;
}
}
class Program
{
static void Main()
{
var u = new User("u1", "a@b.com");
Console.WriteLine(u.Id + " / " + u.Email);
}
}Putting It Together
A well-designed constructor validates input, assigns fields, and leaves the object fully ready to use.
using System;
class Temperature
{
public double Celsius { get; }
public double Fahrenheit { get; }
public Temperature(double celsius)
{
if (celsius < -273.15)
throw new ArgumentException("Below absolute zero");
Celsius = celsius;
Fahrenheit = celsius * 9 / 5 + 32;
}
}
class Program
{
static void Main()
{
var t = new Temperature(25);
Console.WriteLine(t.Celsius + "C = " + t.Fahrenheit + "F");
}
}Quick Check
Test your understanding of constructors.
Recap
A constructor shares the class name, has no return type, and runs on new. Parameterized constructors take initial values; this. disambiguates fields from parameters. You can overload constructors and use default parameter values. Defining any constructor removes the auto-generated default one.
using System;
class Demo
{
public string Name;
public Demo(string name = "anon")
{
this.Name = name;
}
}
class Program
{
static void Main()
{
Console.WriteLine(new Demo().Name);
Console.WriteLine(new Demo("Kai").Name);
}
}Frequently asked questions
Is the “Defining Constructors” lesson free?
Yes — the full text of “Defining Constructors” is free to read here on the web, and the C# Academy course includes 4 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 “Defining Constructors”?
Create instances with parameterized constructors. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining Constructors” 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.
All lessons in this course
- Defining Constructors
- Constructor Chaining with this
- Object and Collection Initializers
- Static Constructors