You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Fix small open issues.
- #33341 already addressed in #36428.
- Fixes#37294: Add text that the shortened format is valid only when the runtime type matches the variable type.
- Fixes#37295: Don't use `ID` in the sample.
- Fixes#37296: Fix nullable warnings. Other issue comments are incorrect.
- Fixes#41748: Change the sample so the constructor is relevant.
- Fixes#42858: Add an explanation on declaring variables in top level statements.
* Remaining open issues
- Fixes#43838: Add notes for collection expressions and using primary constructors. Include naming recommendations.
- Fixes#43839 Update the overview of constructors to include information on primary constructors.
* Code review for style
Fix any style issues with the current code.
* Final edit pass
I also caught a couple sample bits that I'd neglected in the previous commit.
* Apply suggestions from code review
Co-authored-by: Genevieve Warren <[email protected]>
---------
Co-authored-by: Genevieve Warren <[email protected]>
description: A constructor in C# is called when a class or struct is created. Use constructors to set defaults, limit instantiation, and write flexible, easy-to-read code.
4
-
ms.date: 04/06/2023
4
+
ms.date: 01/15/2025
5
5
helpviewer_keywords:
6
6
- "constructors [C#]"
7
7
- "classes [C#], constructors"
8
8
- "C# language, constructors"
9
9
---
10
10
# Constructors (C# programming guide)
11
11
12
-
Whenever an instance of a [class](../../language-reference/keywords/class.md) or a [struct](../../language-reference/builtin-types/struct.md) is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read. For more information and examples, see [Instance constructors](instance-constructors.md) and [Using constructors](using-constructors.md).
12
+
A *constructor* is a method called by the runtime when an instance of a [class](../../language-reference/keywords/class.md) or a [struct](../../language-reference/builtin-types/struct.md) 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](instance-constructors.md) and [Using constructors](using-constructors.md).
13
13
14
-
There are several actions that are part of initializing a new instance. Those actions take place in the following order:
14
+
There are several actions that are part of initializing a new instance. The following actions take place in the following order:
15
15
16
-
1.*Instance fields are set to 0*. This is typically done by the runtime.
16
+
1.*Instance fields are set to 0*. This initialization is typically done by the runtime.
17
17
1.*Field initializers run*. The field initializers in the most derived type run.
18
18
1.*Base type field initializers run*. Field initializers starting with the direct base through each base type to <xref:System.Object?displayProperty=fullName>.
19
19
1.*Base instance constructors run*. Any instance constructors, starting with <xref:System.Object.%23ctor%2A?displayProperty=nameWithType> through each base class to the direct base class.
20
20
1.*The instance constructor runs*. The instance constructor for the type runs.
21
-
1.*Object initializers run*. If the expression includes any object initializers, those run after the instance constructor runs. Object initializers run in the textual order.
21
+
1.*Object initializers run*. If the expression includes any object initializers, they run after the instance constructor runs. Object initializers run in the textual order.
22
22
23
-
The preceding actions take place when a new instance is initialized. If a new instance of a `struct` is set to its `default` value, all instance fields are set to 0.
23
+
The preceding actions take place when an instance is created using the [`new` operator](../../language-reference//operators/new-operator.md). 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.
24
24
25
-
If the [static constructor](static-constructors.md) hasn't run, the static constructor runs before any of the instance constructor actions take place.
25
+
The [static constructor](static-constructors.md), 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.
26
26
27
27
## Constructor syntax
28
28
29
-
A constructor is a method whose name is the same as the name of its type. Its method signature includes only an optional [access modifier](./access-modifiers.md), the method name and its parameter list; it does not include a return type. The following example shows the constructor for a class named `Person`.
29
+
A constructor is a method with the same name as its type. Its method signature can include an optional [access modifier](./access-modifiers.md), 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`.
If a constructor can be implemented as a single statement, you can use an [expression body definition](../statements-expressions-operators/expression-bodied-members.md). 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.
33
+
If a constructor can be implemented as a single statement, you can use an [expression body member](../statements-expressions-operators/expression-bodied-members.md). 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.
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:
The previous examples have all shown instance constructors, which create a new object. A class or struct can also have 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](../../language-reference/builtin-types/default-values.md) article.
43
+
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](../../language-reference/builtin-types/default-values.md) article.
40
44
41
45
The following example uses a static constructor to initialize a static field.
42
46
@@ -48,17 +52,8 @@ You can also define a static constructor with an expression body definition, as
48
52
49
53
For more information and examples, see [Static Constructors](./static-constructors.md).
-[Why Do Initializers Run In The Opposite Order As Constructors? Part One](/archive/blogs/ericlippert/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one)
0 commit comments