11

How can I set up in StyleCop rules that all private fields must begin with underscore _?

Laurel
  • 5,965
  • 14
  • 31
  • 57
Alexander Molodih
  • 1,928
  • 2
  • 20
  • 30

2 Answers2

4

SX1309FieldNamesMustBeginWithUnderscore StyleCop rule gets the job done. https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SX1309.md

This rule can be configured with action error in ruleset file as below to achieve the desired objective.

 <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
       <Rule Id="SX1309" Action="Error" />
 </Rules>
4

You could take a look at StyleCop+ with its flexible naming rules. It is open-source, so you could also use it for writing your own custom rules.

P.S. A similar question: Find out if CsElement is a static field? (StyleCop custom rule)

Community
  • 1
  • 1
Oleg Shuruev
  • 1,339
  • 8
  • 10