124

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
SteveFerg
  • 3,466
  • 7
  • 19
  • 31
  • 7
    Definitely file this feedback to VS. 2017 is a new version with new features, and sometimes these can start off too aggressive. Your feedback will help adjust the default behavior. – Jimmy Nov 29 '16 at 23:55
  • 4
    Sounds like a bug. This is not just about old projects but also about new ones, created in VS2017rc. VS creates the control names it then flags as offensive.. – TaW Dec 15 '16 at 08:49
  • 5
    I encountered this 1006 error with an MSVS-generated button click handler,"btnList_Click()". In other words, MSVS2017 is complaining about a *method it created itself*! It's suddenly complaining about a *naming convention that's been around since .Net 1.0*! Sigh.. IDEAL SOLUTION: `#pragma warning disable IDE1006`. This has the benefit that, since it's in the source code, it's automatically project-wide (you don't have to synchronize any per-workstation MSVS settings). – paulsm4 Mar 17 '19 at 23:42

9 Answers9

170

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.

Jimmy Kueh
  • 15
  • 6
Jason Gabel
  • 1,716
  • 1
  • 10
  • 2
  • 2
    That's exactly the same naming convention that we use for private methods - thanks! Now I just need to work out how to enable this for everyone without people having to individually set this option.... – Matthew Watson Dec 22 '16 at 09:10
  • 13
    A quick way to get to these options is to Click the "Quick Actions" light bulb, hover over "Fix Name Violation" and click the "Change Style Options" button. – Daren Mar 08 '17 at 09:06
  • Yes, didn't think it had worked initially but close and re-open and it looks to have worked. We also use camel case for private methods. – RosieC Mar 24 '17 at 10:57
  • Rebuilding the solution seems to be enough for the new rule to take effect. – Thaoden Mar 26 '17 at 10:56
  • 11
    I just deleted the rules – Paulustrious Apr 16 '17 at 00:33
  • In my case I wan't to actually follow this naming rule, but have data models that does not, and for simplicity and compatibility it makes sense not to fix them. So I have it to none instead of suggestion, as this is actually what I wan't to do, but can't be bothered to be prompted about... Just wish It could be set for the project, not for Visual Studio. – Christopher Bonitz Sep 03 '17 at 12:14
  • 3
    To share such setting/rules/styles with an entire team, refer to this: https://stackoverflow.com/questions/11684457/set-visual-studio-formatting-options-for-an-entire-team – kmote Mar 28 '18 at 14:41
  • 1
    To emphasize what @kmote is saying with their comment, any changes you make in here will NOT go with the solution / project, if you set this then open the solution on a different computer you will get the "suggestions" back again, and other devs on your team will still see them, too (which could cause inconsistencies or unneeded changes in commits if somebody clicks "fix now" accidentally). I don't understand why Microsoft decided to add yet another configuration file. Couldn't they have put naming convention rules in the .ruleset file, or the csproj file? – jrh Jun 08 '18 at 12:56
  • "Close your solution and re-open it for changes to take effect." - not knowing this wasted some minutes for me. Why on earth doesn't VS pop up some message to tell us that?? Especially since this is so inconsistent with how other option changes take effect... – obe May 09 '19 at 15:27
  • 7
    The entire Naming feature appears to be bugged and not functional in the Visual Studio 2019 release. – gpresland May 14 '19 at 20:09
  • I believe this is bugged in VS 2017 community also. I have deleted all my naming conventions, restarted, rebuilt. nothing makes them go away. They were also set to "I"(suggestion) from the beginning(defaults) however they are showing up as errors. Even Alt+enter takes me to a blank rules set (the rules I deleted) however still shows me the error. – diox8tony Oct 11 '21 at 16:41
47

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
Robert S.
  • 1,942
  • 16
  • 22
28

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

enter image description here

A.J.Bauer
  • 2,803
  • 1
  • 26
  • 35
  • 2
    What's the difference between Suppressing them 'In Source' vs 'In Suppression File'? – TylerH Apr 10 '19 at 15:36
  • 3
    @TylerH "In Source" is the file in which the warning is being thrown. "In Suppression File" is a generated file to store all of your warnings. – MistaGoustan Apr 23 '19 at 19:37
14

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);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
8

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.

6

This can be done using normal VS2017 & VS2019 using the .editorconfig settings file, using the naming rules: https://learn.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
Dave Glassborow
  • 3,253
  • 1
  • 30
  • 25
  • I did as you suggested and did not get results! – Dov Miller Jun 13 '21 at 10:20
  • See also this: https://stackoverflow.com/questions/11684457/set-visual-studio-formatting-options-for-an-entire-team/45987894#45987894. Basically, you can add this file to source control so that any other developer in your team uses these settings seamlessly. – Stefano Fenu Apr 02 '22 at 11:21
3

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

3

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

TipVisor
  • 1,022
  • 10
  • 21
1

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.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41