Skip to content

Commit ebf7feb

Browse files
authored
Document the order of initialization actions. (#33769)
* Specify order of initialization Fixes #32809 Fixes #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.
1 parent accf663 commit ebf7feb

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
---
22
title: "Constructors - C# programming guide"
33
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
55
helpviewer_keywords:
66
- "constructors [C#]"
77
- "classes [C#], constructors"
88
- "C# language, constructors"
9-
ms.assetid: df2e2e9d-7998-418b-8e7d-890c17ff6c95
109
---
1110
# Constructors (C# programming guide)
1211

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

1527
## Constructor syntax
1628

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
---
22
title: "Static Constructors - C# Programming Guide"
33
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
55
helpviewer_keywords:
66
- "static constructors [C#]"
77
- "constructors [C#], static"
88
---
99
# Static Constructors (C# Programming Guide)
1010

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

1313
[!code-csharp[SimpleClass#1](snippets/static-constructors/Program.cs#1)]
1414

15+
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+
1523
## Remarks
1624

1725
Static constructors have the following properties:
@@ -21,7 +29,7 @@ Static constructors have the following properties:
2129
- Static constructors cannot be inherited or overloaded.
2230
- A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically.
2331
- 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.
2533
- 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).
2634
- 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.
2735
- 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

Comments
 (0)