Skip to content

Commit b37f6e1

Browse files
authored
static constructor clarifications (#23562)
1 parent c5620cc commit b37f6e1

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
---
22
title: "Static Constructors - C# Programming Guide"
3-
description: A static constructor in C# initializes static data or performs an action done only once before the first instance is created or static members are referenced.
4-
ms.date: 07/20/2015
3+
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: 03/30/2021
55
helpviewer_keywords:
66
- "static constructors [C#]"
77
- "constructors [C#], static"
8-
ms.assetid: 151ec95e-3c4d-4ed7-885d-95b7a3be2e7d
98
---
109
# Static Constructors (C# Programming Guide)
1110

12-
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 once only. 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.
1312

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

1615
## Remarks
1716

1817
Static constructors have the following properties:
1918

20-
- A static constructor does not take access modifiers or have parameters.
19+
- A static constructor doesn't take access modifiers or have parameters.
2120

2221
- A class or struct can only have one static constructor.
2322

@@ -27,15 +26,17 @@ Static constructors have the following properties:
2726

2827
- The user has no control on when the static constructor is executed in the program.
2928

30-
- A static constructor is called automatically to initialize the [class](../../language-reference/keywords/class.md) before the first instance is created or any static members are referenced. A static constructor will run 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 will be executed in the textual order in which they appear in the class declaration immediately prior to the execution of the static constructor.
29+
- 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 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.
3130

3231
- 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).
3332

34-
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running. 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 implicit static constructors that are not explicitly defined in source code, troubleshooting may require inspection of the intermediate language (IL) code.
33+
- 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.
3534

3635
- The presence of a static constructor prevents the addition of the <xref:System.Reflection.TypeAttributes.BeforeFieldInit> type attribute. This limits runtime optimization.
3736

38-
- A field declared as `static readonly` may only be assigned as part of its declaration or in a static constructor. When an explicit static constructor is not required, initialize static fields at declaration, rather than through a static constructor for better runtime optimization.
37+
- A field declared as `static readonly` may only be assigned as part of its declaration or in a static constructor. When an explicit static constructor isn't required, initialize static fields at declaration rather than through a static constructor for better runtime optimization.
38+
39+
- The runtime calls a static constructor no more than once in a single application domain. That call is made in a locked region based on the specific type of the class. No additional locking mechanisms are needed in the body of a static constructor. To avoid the risk of deadlocks, don't block the current thread in static constructors and initializers. For example, don't wait on tasks, threads, wait handles or events, don't acquire locks, and don't execute blocking parallel operations such as parallel loops, `Parallel.Invoke` and Parallel LINQ queries.
3940

4041
> [!Note]
4142
> Though not directly accessible, the presence of an explicit static constructor should be documented to assist with troubleshooting initialization exceptions.
@@ -45,7 +46,7 @@ Static constructors have the following properties:
4546
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
4647
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the `LoadLibrary` method.
4748

48-
- Static constructors are also a convenient place to enforce run-time checks on the type parameter that cannot be checked at compile time via constraints (Type parameter constraints).
49+
- Static constructors are also a convenient place to enforce run-time checks on the type parameter that cannot be checked at compile time via type-parameter constraints.
4950

5051
## Example
5152

0 commit comments

Comments
 (0)