EditorConfig: Allow or Enforce Private Fields to Begin with An Underscore

Viewed 1382

EDIT: I'm using VS Code.

I'm getting the following warning for my code that prefixes private fields with an underscore:

Naming rule violation: Prefix '_' is not expected [MyProject]csharp(IDE1006)

Below is my code:

namespace MyProject
{
    public class Dog
    {
        // Naming rule violation: Prefix '_' is not expected [MyProject]csharp(IDE1006)
        private int _age;

        public int Age()
        {
            return _age;
        }

        public void SetAge(int age)
        {
            _age = age;
        }
    }
}

Below is my .editorconfig file:

[*.cs]

# Require private fields to begin with an underscore (_).
dotnet_naming_rule.instance_fields_should_be_camel_case.severity = warning
dotnet_naming_rule.instance_fields_should_be_camel_case.symbols = instance_fields
dotnet_naming_rule.instance_fields_should_be_camel_case.style = instance_field_style
dotnet_naming_symbols.instance_fields.applicable_kinds = field
dotnet_naming_style.instance_field_style.capitalization = camel_case
dotnet_naming_style.instance_field_style.required_prefix = _

I'm also posting my Directory.Build.props file just in case it's conflicting with my .editorconfig file above. I set it to be as strict as possible so I can fix (or suppress as needed) all warnings that a stricter C# compiler would raise:

<Project>
  <PropertyGroup>
    <Features>strict</Features>
    <WarningLevel>9999</WarningLevel>
  </PropertyGroup>
</Project>

I'm looking for a solution where I can keep C#'s compiler as strict as possible and I can prefix private fields with an underscore, either by allowing them or preferably enforcing them (private fields without an underscore will get a warning).

2 Answers

I finally figured it out after studying Microsoft's documentation here.

Apparently, the middle section of an entry is an identifier and can be set to anything we want.

I have created a new rule that requires private fields to begin with an underscore and be in camel case.

Below is my new .editorconfig file:

# Define what we will treat as private fields.
dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private
# Define rule that something must begin with an underscore and be in camel case.
dotnet_naming_style.require_underscore_prefix_and_camel_case.required_prefix = _
dotnet_naming_style.require_underscore_prefix_and_camel_case.capitalization = camel_case
# Appy our rule to private fields.
dotnet_naming_rule.private_fields_must_begin_with_underscore_and_be_in_camel_case.symbols = private_fields
dotnet_naming_rule.private_fields_must_begin_with_underscore_and_be_in_camel_case.style = require_underscore_prefix_and_camel_case
dotnet_naming_rule.private_fields_must_begin_with_underscore_and_be_in_camel_case.severity = warning

Many thanks to all those who helped, and I hope this helps someone out there.

In Visual Studio you can do this by going to options, then execute the following steps:

  1. Go to "Naming" under C#
  2. Click "Manage naming styles"
  3. Click the + sign
  4. Fill out a logical name
  5. Fill "Required Prefix" with _
  6. Select "Camel Case Name" for Capitalization
  7. Click OK
  8. Click OK
  9. Click the + sign
  10. Select "Private or Internal Field" (if you want private only you need to create a custom specification) as Specification
  11. Select your custom naming style as naming style
  12. Select Warning as severity

Keep in mind these setting will be overwritten by .editorconfig

Related