mafu
mafu

Reputation: 32730

C# Code Analysis dislikes protected static s_Foo (CA1709, CA1707)

I usually add an m_ in front of private fields and an s_ before static members.

With a code like

protected static readonly Random s_Random = new Random ();

I get the following warnings by VS2008's Code Analysis:

How to resolve this issue? Should I simply remove the s_? Or add a global suppression for this warning?

Edit: My company lacks coding standards, so it's up to me to define them for my code. (Yea I know...)

If you think s_ should be removed in general, I'd be glad if you could provide official sources.

Upvotes: 6

Views: 3404

Answers (6)

SkanderTun
SkanderTun

Reputation: 11

  1. Unload the project
  2. Right click on the unloaded project => Edit csproj
  3. Make RunCodeAnalysis to false
  4. Save and reload project

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422280

You are not following Microsoft's .NET naming convention that tells you not to prefix stuff with anything. If this is really what you want, add a suppression. Otherwise, follow the guideline by getting rid of s_ and other similar prefixes.

From Names of Type Members:
"Names of Fields" Section: "Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields."

Upvotes: 11

Stefan Steinegger
Stefan Steinegger

Reputation: 64658

  • CA1709 : protected is not private and needs to be upper case
  • CA1707 : underscores are not according to ms naming convention

Upvotes: 0

Razzie
Razzie

Reputation: 31232

It's up to you how you want to resolve it. Ignore it, and keep your own naming convention, or follow up the Microsoft standard. Personally, I do not use any prefix for my variables (so that would be 'random' instead of 's_Random' in this case) so I would go with the latter, but if you're really comfortable with this, then nobody forces you to change.

Upvotes: 0

cjk
cjk

Reputation: 46475

m_ is an old standard for naming. Newer conventions are to not follow this Hungarian notation.

Upvotes: 0

Gerrie Schenck
Gerrie Schenck

Reputation: 22378

Depends on what you want.

If your company policy is to prefix static members with s_ then you should suppress the warning and even add your own rule.

Otherwise fix it to Microsoft's standards and call your member Random.

Upvotes: 0

Related Questions