Skip to content

Commit 12dcd61

Browse files
authored
Static instance parameters (#48056)
* Update access-modifiers.md Added description for file access type modifier * Update Program.cs Add an example to show parameterless static and instance constructors in a single class. * Update constructors.md Mention Static and instance constructor definiton
1 parent d34a1be commit 12dcd61

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

docs/csharp/programming-guide/classes-and-structs/constructors.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ You can also define a static constructor with an expression body definition, as
5656

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

59+
A class can also have **both** an Instance and Static constructor defined, and they can each be defined without parameters.
60+
61+
:::code source="./snippets/constructors/Program.cs" id="DualConstructor":::
62+
5963
## Partial constructors
6064

6165
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.

docs/csharp/programming-guide/classes-and-structs/snippets/constructors/Program.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,20 @@ public required T Contents
7272
}
7373
// </PrimaryCtor>
7474

75+
// <DualConstructor>
76+
public class Example
77+
{
78+
static Example()
79+
{
80+
Console.WriteLine("Static constructor called.");
81+
}
82+
83+
public Example()
84+
{
85+
Console.WriteLine("Instance constructor called.");
86+
}
87+
88+
// Remaining implementation of Child class.
89+
}
90+
// </DualConstructor>
91+

0 commit comments

Comments
 (0)