Reputation: 5728
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).
Upvotes: 8
Views: 5939
Reputation: 3182
To avoid warnings for const as well, I added these lines to the accepted answer
dotnet_naming_rule.constants_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constants_should_be_pascal_case.symbols = constants
dotnet_naming_rule.constants_should_be_pascal_case.style = constant_style
dotnet_naming_symbols.constants.applicable_kinds = field, local
dotnet_naming_symbols.constants.required_modifiers = const
dotnet_naming_style.constant_style.capitalization = pascal_case
Upvotes: 1
Reputation: 5728
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.
Upvotes: 20
Reputation: 882
In Visual Studio you can do this by going to options, then execute the following steps:
Keep in mind these setting will be overwritten by .editorconfig
Upvotes: 4