Search code examples
c#naming-conventionsnaminglazy-initialization

C# Naming Convention for private properties with backing field


Is there an official naming convention for private properties?
I use them for lazy initialization of expensive fields:

private string _veryExpensive;
private string VeryExpensive => _veryExpensive ??= VeryExpensiveInit();

I feel like since it's private it should be named _veryExpensive, but then it clashes with the backer field name and one of them has to be renamed.

Is it recommended to use PascalCase VeryExpensive or maybe _VeryExpensive for the private property name?

With property-scoped fields I wouldn't have to define another private backing field, but it's not implemented yet: open proposal

I'm not specifically asking for a lazy init, there's other use cases of private properties with a backing field: Are there any reasons to use private properties in C#?


Solution

  • You have the correct naming convention already.

    What you are using is effectively a Expression bodied property. In C#, property names should use Pascal Case.

    There is no difference in the naming conventions for public and private properties, so you are already using the correct convention.