Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions docs/csharp/fundamentals/coding-style/identifier-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`_`).
Comment on lines +40 to +41

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Move the "Private non-constant.." up so it's next to the other non-constant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — moved the "Private non-constant instance fields" bullet up to immediately follow the camelCase non-constant bullet in d0d8c02's successor commit.

- 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.
Expand Down Expand Up @@ -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
Comment on lines +135 to +139
> {
> private const int MaxRetryCount = 3; // PascalCase for constants
> private IWorkerQueue _workerQueue; // _camelCase for non-constant fields
> }
> ```

```csharp
public class DataService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,27 @@
{
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
*/
//</snippet5>
Expand Down Expand Up @@ -719,8 +719,8 @@
//<snippet25>
using (var sr = new System.IO.StreamReader(@"C:\Users\Public\Documents\test.txt"))
{
string s = null;

Check warning on line 722 in docs/csharp/language-reference/keywords/snippets/csrefKeywordsModifiers.cs

View workflow job for this annotation

GitHub Actions / snippets-build

Converting null literal or possible null value to non-nullable type.
while((s = sr.ReadLine()) != null)

Check warning on line 723 in docs/csharp/language-reference/keywords/snippets/csrefKeywordsModifiers.cs

View workflow job for this annotation

GitHub Actions / snippets-build

Converting null literal or possible null value to non-nullable type.
{
Console.WriteLine(s);
}
Expand All @@ -736,7 +736,7 @@
{
// virtual automatically implemented property. Overrides can only
// provide specialized behavior if they implement get and set accessors.
public virtual string Name { get; set; }

Check warning on line 739 in docs/csharp/language-reference/keywords/snippets/csrefKeywordsModifiers.cs

View workflow job for this annotation

GitHub Actions / snippets-build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

// ordinary virtual property with backing field
private int _num;
Expand All @@ -749,7 +749,7 @@

class MyDerivedClass : MyBaseClass
{
private string _name;

Check warning on line 752 in docs/csharp/language-reference/keywords/snippets/csrefKeywordsModifiers.cs

View workflow job for this annotation

GitHub Actions / snippets-build

Non-nullable field '_name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

// Override automatically implemented property with ordinary property
// to provide specialized accessor behavior.
Expand Down
Loading