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
- 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.
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/coding-style/coding-conventions.md
+19-9Lines changed: 19 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: ".NET Coding Conventions"
3
3
description: Learn about commonly used coding conventions in C#. Coding conventions create a consistent look to the code and facilitate copying, changing, and maintaining the code. This article also includes the docs repo coding guidelines
4
-
ms.date: 01/14/2025
4
+
ms.date: 01/15/2025
5
5
helpviewer_keyword:
6
6
- "coding conventions, C#"
7
7
- "Visual C#, coding conventions"
@@ -42,13 +42,13 @@ Code analysis produces warnings and diagnostics when the enabled rules are viola
42
42
The following sections describe practices that the .NET docs team follows to prepare code examples and samples. In general, follow these practices:
43
43
44
44
- Utilize modern language features and C# versions whenever possible.
45
-
- Avoid obsolete or outdated language constructs.
46
-
- Only catch exceptions that can be properly handled; avoid catching generic exceptions.
45
+
- Avoid outdated language constructs.
46
+
- Only catch exceptions that can be properly handled; avoid catching general exceptions. For example, sample code should not catch the <xref:System.Exception?displayProperty=fullName> type without an exception filter.
47
47
- Use specific exception types to provide meaningful error messages.
48
48
- Use LINQ queries and methods for collection manipulation to improve code readability.
49
49
- Use asynchronous programming with async and await for I/O-bound operations.
50
50
- Be cautious of deadlocks and use <xref:System.Threading.Tasks.Task.ConfigureAwait%2A?DisplayProperty=nameWithType> when appropriate.
51
-
- Use the language keywords for data types instead of the runtime types. For example, use `string` instead of <xref:System.String?DisplayProperty=fullName>, or `int` instead of <xref:System.Int32?displayProperty=fullName>.
51
+
- Use the language keywords for data types instead of the runtime types. For example, use `string` instead of <xref:System.String?DisplayProperty=fullName>, or `int` instead of <xref:System.Int32?displayProperty=fullName>. This includes using the types `nint` and `nuint`.
52
52
- Use `int` rather than unsigned types. The use of `int` is common throughout C#, and it's easier to interact with other libraries when you use `int`. Exceptions are for documentation specific to unsigned data types.
53
53
- Use `var` only when a reader can infer the type from the expression. Readers view our samples on the docs platform. They don't have hover or tool tips that display the type of variables.
54
54
- Write code with clarity and simplicity in mind.
@@ -66,15 +66,25 @@ More specific guidelines follow.
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/classes-and-structs/constructors.md
+10-6Lines changed: 10 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
---
2
2
title: "Constructors"
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: 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 that is 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 may have multiple constructors that take different arguments. Constructors enable you to set 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
14
There are several actions that are part of initializing a new instance. Those actions take place in the following order:
15
15
@@ -20,23 +20,27 @@ There are several actions that are part of initializing a new instance. Those ac
20
20
1.*The instance constructor runs*. The instance constructor for the type runs.
21
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
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`.
24
24
25
25
If the [static constructor](static-constructors.md) hasn't run, the static constructor runs before any of the instance constructor actions take place.
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 declared using the same as the name of its type. Its method signature can include 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`.
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 arguments 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 have shown instance constructors, which create 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.
0 commit comments