Search code examples
c#visual-studio-codewarningssuppress-warningseditorconfig

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


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


Solution

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