Skip to content

Add naming conventions for primary constructor parameters #48009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 20, 2025
Merged
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
17 changes: 17 additions & 0 deletions docs/csharp/fundamentals/coding-style/identifier-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: "Identifier names - rules and conventions"
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."
ms.date: 11/27/2023
ai-usage: ai-assisted
---
# C# identifier naming rules and conventions

Expand Down Expand Up @@ -159,6 +160,22 @@ public T SomeMethod<T>(int someNumber, bool isValid)
}
```

#### Primary constructor parameters

How you name primary constructor parameters depends on the type being declared:

- For `class` and `struct` types: Use camel casing, consistent with other method parameters.

:::code language="csharp" source="./snippets/identifier-names/PrimaryConstructorExamples.cs" id="ClassPrimaryConstructor":::

:::code language="csharp" source="./snippets/identifier-names/PrimaryConstructorExamples.cs" id="StructPrimaryConstructor":::

- For `record` types: Use Pascal casing, as the parameters become public properties.

:::code language="csharp" source="./snippets/identifier-names/PrimaryConstructorExamples.cs" id="RecordPrimaryConstructor":::

For more information on primary constructors, see [Primary constructors](../../programming-guide/classes-and-structs/instance-constructors.md#primary-constructors).

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

### Type parameter naming guidelines
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Microsoft.Extensions.Logging;

namespace IdentifierNamingExamples;

// <ClassPrimaryConstructor>
public class DataService(IWorkerQueue workerQueue, ILogger logger)
{
public void ProcessData()
{
// Use the parameters directly
logger.LogInformation("Processing data");
workerQueue.Enqueue("data");
}
}
// </ClassPrimaryConstructor>

// <StructPrimaryConstructor>
public struct Point(double x, double y)
{
public double Distance => Math.Sqrt(x * x + y * y);
}
// </StructPrimaryConstructor>

// <RecordPrimaryConstructor>
public record Person(string FirstName, string LastName);
public record Address(string Street, string City, string PostalCode);
// </RecordPrimaryConstructor>

// Supporting interfaces for examples
public interface IWorkerQueue
{
void Enqueue(string item);
int Count { get; }
}

// Add a simple main method to make it an executable
public class Program
{
public static void Main()
{
// Example usage
var person = new Person("John", "Doe");
var address = new Address("123 Main St", "Anytown", "12345");
Console.WriteLine($"{person.FirstName} {person.LastName}");
Console.WriteLine($"{address.Street}, {address.City} {address.PostalCode}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<OutputType>Exe</OutputType>
<RootNamespace>IdentifierNamingExamples</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

</Project>