How to get rid of Naming rule violation messages in Visual Studio?

C#asp.netVisual Studio-2017

C# Problem Overview


I just installed Visual Studio 2017. When I open an existing website, I get all sorts of warning messages such as this one:

> IDE1006 Naming rule violation: These words must begin with upper case > characters: swe_calc

In the code it is defined as:

[System.Runtime.InteropServices.DllImport("swedll32.dll")]
public static extern Int32 swe_calc(double tjd, int ipl, Int32 iflag, IntPtr xx, IntPtr serr);

This also occurs with my ASP.Net controls. As an example of a DropDownList:

> IDE1006 Naming rule violation: These words must begin with upper case > characters: ddlMonth_SelectedIndexChanged

How can I eliminate these type of warnings under Visual Studio?

C# Solutions


Solution 1 - C#

Its a new configurable feature, if you go to

>Tools → Options → Text Editor → Your language (I did C#) → Code Style → Naming

In there I went to Manage Styles add camel Case (its in there but you need to add it to your selectable): go to the "+" sign, then add your rule accordingly.

Important: Close your solution and re-open it for changes to take effect.

For example, I only use camel Case for private methods. So I choose Private Method and required Style the new one I created "camel Case" and set it to Severity Suggestion (I also promoted it to the top).

The built in are all "Suggestions" too so you can also just turn off Messages.

Solution 2 - C#

If you want to suppress it only in some files or areas you can use the following:

#pragma warning disable IDE1006

// the code with the warning

#pragma warning restore IDE1006

Solution 3 - C#

If you need to get rid of these messages you could also just suppress them.

enter image description here

Solution 4 - C#

You could rename the method and add the name to the attribute with the EntryPoint property.

[System.Runtime.InteropServices.DllImport("swedll32.dll", EntryPoint = "swe_calc")]
public static extern Int32 SweCalc(double tjd, int ipl, Int32 iflag, IntPtr xx, IntPtr serr);

Solution 5 - C#

If you want to omit or void the warning message in a method, you can use the SuppressMessage from the namespace System.Diagnostics.CodeAnalysis:

[SuppressMessage("Microsoft.Design", "IDE1006", Justification = "Rule violation aceppted due blah blah..")]

The Justification property is optional, but it's worth spending a moment writing a reason, to let your team know that the code is revised and is ok.

Solution 6 - C#

This can be done using normal VS2017 & VS2019 using the .editorconfig settings file, using the naming rules: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference

The file can be created by hand, or in VS2019 you can get Visual Studio to create it for your from your preferences (i.e. after following configuring your prefs as in https://stackoverflow.com/a/41131563/131701 ), by hitting the generate editor config file from settings button.

generate editor config file from settings button

For example, the following sets of rules will enable camelCase for all non public methods, and keep the other default naming rules that comes with VS.

#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.private_method_should_be_camelcasestyle.severity = suggestion
dotnet_naming_rule.private_method_should_be_camelcasestyle.symbols = private_method
dotnet_naming_rule.private_method_should_be_camelcasestyle.style = camelcasestyle

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal
dotnet_naming_symbols.interface.required_modifiers = 

dotnet_naming_symbols.private_method.applicable_kinds = method
dotnet_naming_symbols.private_method.applicable_accessibilities = private, protected, internal, protected_internal
dotnet_naming_symbols.private_method.required_modifiers = 

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal
dotnet_naming_symbols.types.required_modifiers = 

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal
dotnet_naming_symbols.non_field_members.required_modifiers = 

# Naming styles

dotnet_naming_style.pascal_case.required_prefix = 
dotnet_naming_style.pascal_case.required_suffix = 
dotnet_naming_style.pascal_case.word_separator = 
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix = 
dotnet_naming_style.begins_with_i.word_separator = 
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.camelcasestyle.required_prefix = 
dotnet_naming_style.camelcasestyle.required_suffix = 
dotnet_naming_style.camelcasestyle.word_separator = 
dotnet_naming_style.camelcasestyle.capitalization = camel_case

Solution 7 - C#

If you hover over the naming rule violation, you can use Alt + Enter to bring up the naming styles for that language. You can also use Tools -> Options -> Text Editor -> {language} -> Code Style -> Naming.

For camelCase rules on Methods, you can add a new rule and set that to Camel Case. When you close the code file and open it up again, you shouldn't see that warning anymore. Not sure why this isn't a default option, but it wasn't in my case (using Visual Code 15.8). I had to edit styles to match our company standards.

Sample C# Naming Styles Settings

Solution 8 - C#

disable the rule. rightclick error message and select severity to none

Solution 9 - C#

What this rule asserts is that fields must be private.

You can convert it to a Property by adding the {get;set;} after the field.

This removed the error for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteveFergView Question on Stackoverflow
Solution 1 - C#Jason GabelView Answer on Stackoverflow
Solution 2 - C#Robert S.View Answer on Stackoverflow
Solution 3 - C#A.J.BauerView Answer on Stackoverflow
Solution 4 - C#Daniel A. WhiteView Answer on Stackoverflow
Solution 5 - C#Carlos David LópezView Answer on Stackoverflow
Solution 6 - C#Dave GlassborowView Answer on Stackoverflow
Solution 7 - C#Greg McFallsView Answer on Stackoverflow
Solution 8 - C#TipVisorView Answer on Stackoverflow
Solution 9 - C#mosborn1987View Answer on Stackoverflow