diff --git a/docs/csharp/fundamentals/coding-style/identifier-names.md b/docs/csharp/fundamentals/coding-style/identifier-names.md index b5dd421dad868..7290527f4c57c 100644 --- a/docs/csharp/fundamentals/coding-style/identifier-names.md +++ b/docs/csharp/fundamentals/coding-style/identifier-names.md @@ -37,9 +37,9 @@ By convention, C# programs use `PascalCase` for type names, namespaces, and all - Use meaningful and descriptive names for variables, methods, and classes. - Prefer clarity over brevity. - Use PascalCase for class names and method names. -- Use camelCase for method arguments, local variables, and private fields. -- Use PascalCase for constant names, both fields and local constants. -- Private instance fields start with an underscore (`_`). +- Use camelCase for method arguments, local variables, and private non-constant fields. +- Private non-constant instance fields start with an underscore (`_`). +- To maintain consistency across all access modifiers, use PascalCase for constant names, both fields and local constants, including `private` and `internal` constants. - Static fields start with `s_`. This convention isn't the default Visual Studio behavior, nor part of the [Framework design guidelines](../../../standard/design-guidelines/names-of-type-members.md#names-of-fields), but is [configurable in editorconfig](../../../fundamentals/code-analysis/style-rules/naming-rules.md). - Avoid using abbreviations or acronyms in names, except for widely known and accepted abbreviations. - Use meaningful and descriptive namespaces that follow the reverse domain name notation. @@ -130,7 +130,18 @@ For more information on positional records, see [Positional syntax for property ### Camel case -Use camel casing ("camelCasing") when naming `private` or `internal` fields and prefix them with `_`. Use camel casing when naming local variables, including instances of a delegate type. +Use camel casing ("camelCasing") when naming `private` or `internal` non-constant fields, and prefix them with `_`. Use camel casing when naming local variables, including instances of a delegate type. + +> [!NOTE] +> Private and internal constants use PascalCase (not `_camelCase`) to maintain consistency with constant naming conventions. For example: +> +> ```csharp +> public class DataService +> { +> private const int MaxRetryCount = 3; // PascalCase for constants +> private IWorkerQueue _workerQueue; // _camelCase for non-constant fields +> } +> ``` ```csharp public class DataService diff --git a/docs/csharp/language-reference/keywords/snippets/csrefKeywordsModifiers.cs b/docs/csharp/language-reference/keywords/snippets/csrefKeywordsModifiers.cs index 7c058a3e6ab73..e1bb160af7fe5 100644 --- a/docs/csharp/language-reference/keywords/snippets/csrefKeywordsModifiers.cs +++ b/docs/csharp/language-reference/keywords/snippets/csrefKeywordsModifiers.cs @@ -177,27 +177,27 @@ public class ConstTest { class SampleClass { - public int x; - public int y; + public int X; + public int Y; public const int C1 = 5; public const int C2 = C1 + 5; public SampleClass(int p1, int p2) { - x = p1; - y = p2; + X = p1; + Y = p2; } } static void Main() { var mC = new SampleClass(11, 22); - Console.WriteLine($"x = {mC.x}, y = {mC.y}"); + Console.WriteLine($"X = {mC.X}, Y = {mC.Y}"); Console.WriteLine($"C1 = {SampleClass.C1}, C2 = {SampleClass.C2}"); } } /* Output - x = 11, y = 22 + X = 11, Y = 22 C1 = 5, C2 = 10 */ //