How can I set up in StyleCop rules that all private fields must begin with underscore _
?
Asked
Active
Viewed 4,200 times
11

Laurel
- 5,965
- 14
- 31
- 57

Alexander Molodih
- 1,928
- 2
- 20
- 30
-
1Add also rule that every function should contain at least one goto statement:) – Alex F Nov 14 '11 at 10:52
-
[This](http://scottwhite.blogspot.com/2008/11/creating-custom-stylecop-rules-in-c.html) might be useful for you with a few minor changes. – Johannes Kommer Nov 14 '11 at 11:13
2 Answers
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>

Surender Singh Malik
- 295
- 3
- 12
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
-
1That link doesn't work and I can't turn up much -- is StyleCop+ no more? – MikeB Mar 21 '22 at 17:36