C# Constructors: Types, Examples, and Advanced Patterns
C# Constructors
Discover how constructors initialize objects in C#, covering all constructor types, patterns, and best practices.
In C#, a constructor is a special method invoked automatically when a new instance of a class is created. Unlike regular methods, constructors:
- share the same name as the class
- do not return a value
Creating a C# Constructor
Define a constructor within a class by matching the class name:
class Car {
// constructor
Car() {
// initialization code
}
}
Here, Car() is the constructor. It is invoked using the new keyword:
new Car();
For instance:
Car car1 = new Car();
See also: C# Class and Objects.
Types of Constructors
Typical constructor categories include:
- Parameterless (default) constructor
- Parameterized constructor
- Copy constructor
- Private constructor
- Static constructor
1. Parameterless Constructor
A constructor without parameters is called a parameterless or default constructor. Example:
using System;
namespace ConstructorDemo {
class Car {
// parameterless constructor
Car() {
Console.WriteLine("Car Constructor");
}
static void Main(string[] args) {
new Car();
Console.ReadLine();
}
}
}
Output
Car Constructor
2. Parameterized Constructor
Constructors can accept arguments to initialize instance fields:
using System;
namespace ConstructorDemo {
class Car {
string brand;
int price;
// parameterized constructor
Car(string theBrand, int thePrice) {
brand = theBrand;
price = thePrice;
}
static void Main(string[] args) {
Car car1 = new Car("Bugatti", 50000);
Console.WriteLine("Brand: " + car1.brand);
Console.WriteLine("Price: " + car1.price);
Console.ReadLine();
}
}
}
Output
Brand: Bugatti Price: 50000
3. Default Constructor
If you omit a constructor, the compiler supplies an implicit parameterless constructor that initializes fields to their default values. Example:
using System;
namespace ConstructorDemo {
class Program {
int a;
static void Main(string[] args) {
Program p1 = new Program();
Console.WriteLine("Default value of a: " + p1.a);
Console.ReadLine();
}
}
}
Output
Default value of a: 0
Numeric fields default to 0, while strings and objects default to null.
4. Copy Constructor
A copy constructor creates a new instance by copying another object’s state:
using System;
namespace ConstructorDemo {
class Car {
string brand;
// primary constructor
Car(string theBrand) { brand = theBrand; }
// copy constructor
Car(Car c1) { brand = c1.brand; }
static void Main(string[] args) {
Car car1 = new Car("Bugatti");
Console.WriteLine("Brand of car1: " + car1.brand);
Car car2 = new Car(car1);
Console.WriteLine("Brand of car2: " + car2.brand);
Console.ReadLine();
}
}
}
Output
Brand of car1: Bugatti Brand of car2: Bugatti
5. Private Constructor
Marking a constructor private prevents external instantiation, useful for singleton patterns or utility classes.
Example
using System;
namespace ConstructorDemo {
class Car {
private Car() { Console.WriteLine("Private Constructor"); }
}
class CarDrive {
static void Main(string[] args) {
// Car car1 = new Car(); // compile‑time error
Console.ReadLine();
}
}
}
The error:
error CS0122: 'Car.Car()' is inaccessible due to its protection level
Note: When a constructor is private, all class members should be static to allow access via the class name.
6. Static Constructor
A static constructor runs once per type, before any instance or static member is accessed. It cannot be called directly and must have no parameters.
using System;
namespace ConstructorDemo {
class Car {
static Car() {
Console.WriteLine("Static Constructor");
}
Car() { Console.WriteLine("Default Constructor"); }
static void Main(string[] args) {
Car car1 = new Car();
Car car2 = new Car();
Console.ReadLine();
}
}
}
Output
Static Constructor Default Constructor Default Constructor
Note: Only one static constructor per class; it cannot have parameters or access modifiers.
C# Constructor Overloading
Define multiple constructors with differing parameter lists to provide flexible object creation. Example:
using System;
namespace ConstructorOverloadDemo {
class Car {
Car() { Console.WriteLine("Car constructor"); }
Car(string brand) {
Console.WriteLine("Car constructor with one parameter");
Console.WriteLine("Brand: " + brand);
}
static void Main(string[] args) {
Car car = new Car();
Console.WriteLine();
Car car2 = new Car("Bugatti");
Console.ReadLine();
}
}
}
Output
Car constructor Car constructor with one parameter Brand: Bugatti
The compiler selects the matching constructor based on the argument count and types. Learn more at C# Constructor Overloading.
C Language
- Understanding C# Nested Classes: Definition, Usage, and Inheritance
- Mastering Constructor Overloading in C#
- Understanding C++ Constructors: Default, Parameterized, and Copy Explained
- Master Java Constructors: Types, Usage, and Practical Examples
- Java Enum Constructors Explained with Practical Example
- Understanding Car Engine Coolant: Purpose, Types, and Maintenance
- Understanding Java Constructors: Initialization and Best Practices
- Transform Your RC Car Into a Bluetooth‑Controlled Vehicle
- Qi-Enabled Wi‑Fi RC Car: Build a Wireless Remote‑Control Car Kit
- Build Your Own RC Porsche Car with Arduino: A Step‑by‑Step Guide