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
Document the order of initialization actions. (#33769)
* Specify order of initialization
Fixes#32809Fixes#33704
Specify the order of initialization actions when static constructors run, and when a new instance is created.
* Add link from constructor to static constructor.
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/classes-and-structs/constructors.md
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,28 @@
1
1
---
2
2
title: "Constructors - C# programming guide"
3
3
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: 09/27/2021
4
+
ms.date: 01/30/2023
5
5
helpviewer_keywords:
6
6
- "constructors [C#]"
7
7
- "classes [C#], constructors"
8
8
- "C# language, constructors"
9
-
ms.assetid: df2e2e9d-7998-418b-8e7d-890c17ff6c95
10
9
---
11
10
# Constructors (C# programming guide)
12
11
13
-
Whenever a [class](../../language-reference/keywords/class.md) or [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
+
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).
13
+
14
+
There are several actions that are part of initializing a new instance. Those actions take place in the following order:
15
+
16
+
1.*Instance fields are set to 0*. This is typically done by the runtime.
17
+
1.*Field initializers run*. The field initializers in the most derived type run.
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
+
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
+
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.
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.
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.
description: A static constructor in C# initializes static data or performs an action done only once. It runs before the first instance is created or static members are referenced.
4
-
ms.date: 12/21/2021
4
+
ms.date: 01/30/2023
5
5
helpviewer_keywords:
6
6
- "static constructors [C#]"
7
7
- "constructors [C#], static"
8
8
---
9
9
# Static Constructors (C# Programming Guide)
10
10
11
-
A static constructor is used to initialize any [static](../../language-reference/keywords/static.md) data, or to perform a particular action that needs to be performed only once. It is called automatically before the first instance is created or any static members are referenced.
11
+
A static constructor is used to initialize any [static](../../language-reference/keywords/static.md) data, or to perform a particular action that needs to be performed only once. It is called automatically before the first instance is created or any static members are referenced. A static constructor will be called at most once.
There are several actions that are part of static initialization. Those actions take place in the following order:
16
+
17
+
1.*Static fields are set to 0*. This is typically done by the runtime.
18
+
1.*Static field initializers run*. The static field initializers in the most derived type run.
19
+
1.*Base type static field initializers run*. Static field initializers starting with the direct base through each base type to <xref:System.Object?displayProperty=fullName>.
20
+
1.*Base static constructors run*. Any static constructors, starting with <xref:System.Object.%23ctor%2A?displayProperty=nameWithType> through each base class to the direct base class.
21
+
1.*The static constructor runs*. The static constructor for the type runs.
22
+
15
23
## Remarks
16
24
17
25
Static constructors have the following properties:
@@ -21,7 +29,7 @@ Static constructors have the following properties:
21
29
- Static constructors cannot be inherited or overloaded.
22
30
- A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically.
23
31
- The user has no control on when the static constructor is executed in the program.
24
-
- A static constructor is called automatically. It initializes the [class](../../language-reference/keywords/class.md) before the first instance is created or any static members declared in that class (not its base classes) are referenced. A static constructor runs before an instance constructor. A type's static constructor is called when a static method assigned to an event or a delegate is invoked and not when it is assigned. If static field variable initializers are present in the class of the static constructor, they're executed in the textual order in which they appear in the class declaration. The initializers run immediately prior to the execution of the static constructor.
32
+
- A static constructor is called automatically. It initializes the [class](../../language-reference/keywords/class.md) before the first instance is created or any static members declared in that class (not its base classes) are referenced. A static constructor runs before an instance constructor. If static field variable initializers are present in the class of the static constructor, they're executed in the textual order in which they appear in the class declaration. The initializers run immediately prior to the execution of the static constructor.
25
33
- If you don't provide a static constructor to initialize static fields, all static fields are initialized to their default value as listed in [Default values of C# types](../../language-reference/builtin-types/default-values.md).
26
34
- If a static constructor throws an exception, the runtime doesn't invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain. Most commonly, a <xref:System.TypeInitializationException> exception is thrown when a static constructor is unable to instantiate a type or for an unhandled exception occurring within a static constructor. For static constructors that aren't explicitly defined in source code, troubleshooting may require inspection of the intermediate language (IL) code.
27
35
- The presence of a static constructor prevents the addition of the <xref:System.Reflection.TypeAttributes.BeforeFieldInit> type attribute. This limits runtime optimization.
0 commit comments