Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Default Constructor: How It Works and When to Use It

C# default constructors: how they are generated, when the compiler stops generating them, and how to control initialization in classes and structs.

~4 min readC#
#C##constructors#object initialization#.NET
Illustration of a C# class with an implicit default constructor initializing fields to default values.

In C#, a default constructor is a parameterless constructor. For a class, the compiler generates one automatically only when you do not declare any other constructor. The generated constructor calls the base class's parameterless constructor, runs any field initializers, and leaves any fields without initializers at their type's default value. Default constructors are convenient, but they also affect inheritance, serialization, dependency injection, and how you design types that must preserve invariants.

What the Compiler Generates for a Class

When you define a non-static class without any constructor, the C# compiler adds a parameterless constructor. For a non-abstract class, that generated constructor is public. For example:

csharp
public class Order { public int Id { get; set; } public string CustomerName { get; set; } }

The class above has a generated constructor equivalent to:

csharp
public Order() : base() { }

This constructor calls the base class's parameterless constructor (object's parameterless constructor if no base class is specified). Field initializers run before the constructor body, and fields without initializers receive the type's default value. The generated constructor is public, so you can instantiate the class with new Order().

When the Compiler Does Not Generate One

If you declare any instance constructor on a class, even one with parameters, the compiler no longer generates a parameterless constructor. This is a common source of confusion. For example:

csharp
public class Invoice { public Invoice(decimal amount) { Amount = amount; } public decimal Amount { get; set; } }

Now new Invoice() does not compile because no parameterless constructor exists. If you still need one, you must add it explicitly:

csharp
public Invoice() { }

This behavior matters because a custom constructor signals that the class requires specific initialization. The compiler cannot assume that a parameterless version is safe.

Default Constructors in Structs

Structs behave differently. In versions of C# before 10, every struct has an implicit public parameterless constructor that you cannot override. It initializes all fields to their default values. For example:

csharp
public struct Point { public int X; public int Y; }

You can always call new Point() and get X and Y set to 0. In C# 10 and later, you can define an explicit parameterless constructor for a struct, but it must assign a value to every field. That gives structs more flexibility while preserving value-type semantics.

Field Initialization and Default Values

The default constructor runs field initializers before the constructor body. Field initializers are expressions assigned at declaration:

csharp
public class Settings { public int Timeout = 30; public string Name = "default"; }

When the default constructor is used, Timeout becomes 30 and Name becomes "default". Without field initializers, the fields get the type's default value: 0 for int, null for string, and so on. This distinction matters when you later add a parameterized constructor: field initializers still run, but the constructor body can override them.

Overriding the Default Constructor

You can replace the implicit default constructor by declaring an explicit parameterless constructor. This is useful when you need to set up a known state or validate invariants:

csharp
public class Repository { private readonly string _connectionString; public Repository() { _connectionString = "localhost:5432"; } }

Once you do this, the compiler no longer generates any other constructor. If you also want a constructor that accepts a connection string, you must add it separately and decide whether it chains to the parameterless one.

Inheritance and Constructor Chaining

When a derived class uses its default constructor, it implicitly calls the base class's parameterless constructor. If the base class does not have a parameterless constructor, the derived class must explicitly call a base constructor using base(...). For example:

csharp
public class Base { public Base(string name) { } } public class Derived : Base { public Derived() : base("default") { } }

If you rely on the implicit default constructor in a derived class, the base class must have a parameterless constructor. Otherwise, compilation fails. This is a common design constraint when adding constructors to base classes.

Practical Implications for Serialization and Dependency Injection

Frameworks that instantiate types automatically vary in what they require. Some serializers and DI containers expect a public parameterless constructor; others can use a parameterized constructor or resolve constructor arguments from a container. For example, ASP.NET Core's DI container normally does not require a parameterless constructor; it can activate a public constructor by supplying registered dependencies. Recent versions of System.Text.Json also support parameterized constructors. Some reflection-based tools still assume a public parameterless constructor, so keeping one is often a useful compatibility convention, especially for types that may be activated by tools you do not control. However, a parameterless constructor that leaves fields in an invalid state can cause bugs. Consider whether you need to initialize fields to safe defaults or use nullable reference types to signal uninitialized state.

Common Mistakes and How to Avoid Them

A frequent mistake is adding a parameterized constructor and forgetting that the default constructor disappears. This breaks code that relied on new MyClass(), including serialization and reflection-based instantiation. Another mistake is assuming that a struct's explicit parameterless constructor is available in older C# versions. Always verify the language version you are targeting. When you do define a parameterless constructor, ensure it does not leave the object in a partially initialized state that violates invariants. If the class is meant to be immutable, consider making the constructor the only way to set fields.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.