Skip to content

Commit c6c53f1

Browse files
CopilotBillWagnergewarren
authored
Add naming conventions for primary constructor parameters (#48009)
* Initial plan * Add naming rules for primary constructor parameters Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 28489ed commit c6c53f1

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

docs/csharp/fundamentals/coding-style/identifier-names.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: "Identifier names - rules and conventions"
33
description: "Learn the rules for valid identifier names in the C# programming language. In addition, learn the common naming conventions used by the .NET runtime team and the .NET docs team."
44
ms.date: 11/27/2023
5+
ai-usage: ai-assisted
56
---
67
# C# identifier naming rules and conventions
78

@@ -159,6 +160,22 @@ public T SomeMethod<T>(int someNumber, bool isValid)
159160
}
160161
```
161162

163+
#### Primary constructor parameters
164+
165+
How you name primary constructor parameters depends on the type being declared:
166+
167+
- For `class` and `struct` types: Use camel casing, consistent with other method parameters.
168+
169+
:::code language="csharp" source="./snippets/identifier-names/PrimaryConstructorExamples.cs" id="ClassPrimaryConstructor":::
170+
171+
:::code language="csharp" source="./snippets/identifier-names/PrimaryConstructorExamples.cs" id="StructPrimaryConstructor":::
172+
173+
- For `record` types: Use Pascal casing, as the parameters become public properties.
174+
175+
:::code language="csharp" source="./snippets/identifier-names/PrimaryConstructorExamples.cs" id="RecordPrimaryConstructor":::
176+
177+
For more information on primary constructors, see [Primary constructors](../../programming-guide/classes-and-structs/instance-constructors.md#primary-constructors).
178+
162179
For more information on C# naming conventions, see the [.NET Runtime team's coding style](https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/coding-style.md).
163180

164181
### Type parameter naming guidelines
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace IdentifierNamingExamples;
5+
6+
// <ClassPrimaryConstructor>
7+
public class DataService(IWorkerQueue workerQueue, ILogger logger)
8+
{
9+
public void ProcessData()
10+
{
11+
// Use the parameters directly
12+
logger.LogInformation("Processing data");
13+
workerQueue.Enqueue("data");
14+
}
15+
}
16+
// </ClassPrimaryConstructor>
17+
18+
// <StructPrimaryConstructor>
19+
public struct Point(double x, double y)
20+
{
21+
public double Distance => Math.Sqrt(x * x + y * y);
22+
}
23+
// </StructPrimaryConstructor>
24+
25+
// <RecordPrimaryConstructor>
26+
public record Person(string FirstName, string LastName);
27+
public record Address(string Street, string City, string PostalCode);
28+
// </RecordPrimaryConstructor>
29+
30+
// Supporting interfaces for examples
31+
public interface IWorkerQueue
32+
{
33+
void Enqueue(string item);
34+
int Count { get; }
35+
}
36+
37+
// Add a simple main method to make it an executable
38+
public class Program
39+
{
40+
public static void Main()
41+
{
42+
// Example usage
43+
var person = new Person("John", "Doe");
44+
var address = new Address("123 Main St", "Anytown", "12345");
45+
Console.WriteLine($"{person.FirstName} {person.LastName}");
46+
Console.WriteLine($"{address.Street}, {address.City} {address.PostalCode}");
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<RootNamespace>IdentifierNamingExamples</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)