Constructors (C# programming guide)
A constructor is a method called by the runtime when an instance of a class or a struct is created. A class or struct can have multiple constructors that take different arguments. Constructors enable you to ensure that instances of the type are valid when created. For more information and examples, see Instance constructors and Using constructors.
There are several actions that are part of initializing a new instance. The following actions take place in the following order:
- Instance fields are set to 0. This initialization is typically done by the runtime.
- Field initializers run. The field initializers in the most derived type run.
- Base type field initializers run. Field initializers starting with the direct base through each base type to System.Object.
- Base instance constructors run. Any instance constructors, starting with Object.Object through each base class to the direct base class.
- The instance constructor runs. The instance constructor for the type runs.
- Object initializers run. If the expression includes any object initializers, they run after the instance constructor runs. Object initializers run in the textual order.
The preceding actions take place when an instance is created using the new
operator. If a new instance of a struct
is set to its default
value, all instance fields are set to 0. Elements of an array are set to their default value of 0 or null
when an array is created.
The static constructor, if any, runs before any of the instance constructor actions take place for any instance of the type. The static constructor runs at most once.
Constructor syntax
A constructor is a method with the same name as its type. Its method signature can include an optional access modifier, the method name, and its parameter list; it doesn't include a return type. The following example shows the constructor for a class named Person
.
public class Person
{
private string last;
private string first;
public Person(string lastName, string firstName)
{
last = lastName;
first = firstName;
}
// Remaining implementation of Person class.
}
If a constructor can be implemented as a single statement, you can use an expression body member. The following example defines a Location
class whose constructor has a single string parameter named name. The expression body definition assigns the argument to the locationName
field.
public class Location
{
private string locationName;
public Location(string name) => Name = name;
public string Name
{
get => locationName;
set => locationName = value;
}
}
If a type requires a parameter to create an instance, you can use a primary constructor to indicate that one or more parameters are required to instantiate the type, as shown in the following example:
public class LabelledContainer<T>(string label)
{
public string Label { get; } = label;
public required T Contents
{
get;
init;
}
}
Static constructors
The previous examples show instance constructors, which initialize a new object. A class or struct can also declare a static constructor, which initializes static members of the type. Static constructors are parameterless. If you don't provide a static constructor to initialize static fields, the C# compiler initializes static fields to their default value as listed in the Default values of C# types article.
The following example uses a static constructor to initialize a static field.
public class Adult : Person
{
private static int minimumAge;
public Adult(string lastName, string firstName) : base(lastName, firstName)
{ }
static Adult() => minimumAge = 18;
// Remaining implementation of Adult class.
}
You can also define a static constructor with an expression body definition, as the following example shows.
public class Child : Person
{
private static int maximumAge;
public Child(string lastName, string firstName) : base(lastName, firstName)
{ }
static Child() => maximumAge = 18;
// Remaining implementation of Child class.
}
For more information and examples, see Static Constructors.