Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ You can also define a static constructor with an expression body definition, as

For more information and examples, see [Static Constructors](./static-constructors.md).

A class can also have **both** an Instance and Static constructor defined, and they can each be defined without parameters.

:::code source="./snippets/constructors/Program.cs" id="DualConstructor":::

## Partial constructors

Beginning with C# 14, you can declare *partial constructors* in a partial class or struct. Any partial constructor must have a *defining declaration* and an *implementing declaration*. The signatures of the declaring and implementing partial constructors must match according to the rules of [partial members](./partial-classes-and-methods.md#partial-members). The defining declaration of the partial constructor can't use the `: base()` or `: this()` constructor initializer. You add any constructor initializer must be added on the implementing declaration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,20 @@ public required T Contents
}
// </PrimaryCtor>

// <DualConstructor>
public class Example
{
static Example()
{
Console.WriteLine("Static constructor called.");
}

public Example()
{
Console.WriteLine("Instance constructor called.");
}

// Remaining implementation of Child class.
}
// </DualConstructor>

Loading