Search code examples
c#c#-12.0primary-constructor

Do I need underscore in primary constructor parameters' names?


What is right:

class MyClass(IInjectable _injectable) {}

or

class MyClass(IInjectable injectable) {}

On the one hand, we have private field. On the other hand, this is constructor parameter's name.


Solution

  • Primary constructor parameters should best not be regarded as "implicit private fields", they are not members of the class. Hence, your first notation makes little sense.

    See https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/primary-constructors for details.