6

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:

  • CA1709: Microsoft.Naming : Correct the casing of 's' in member name 'Bar.s_Random' by changing it to 'S'.
  • CA1707: Microsoft.Naming : Remove the underscores from member name 'Bar.s_Random'.

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.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
mafu
  • 31,798
  • 42
  • 154
  • 247

6 Answers6

11

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."

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
1
  1. Unload the project
  2. Right click on the unloaded project => Edit csproj
  3. Make RunCodeAnalysis to false
  4. Save and reload project
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
SkanderTun
  • 11
  • 1
  • i maybe did not understood the question very well, but i wrote that answer because i did it and that solution helped me. There was a VS update that made my project not able to build, and the company used a specefic naming convention that i couldnt change. So the solution was useful – SkanderTun Dec 07 '21 at 14:40
0

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.

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
0

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

cjk
  • 45,739
  • 9
  • 81
  • 112
  • To my knowledge there is no clear consensus on this topic. Could you provide some sources of a deprecation of m_? – mafu Apr 27 '09 at 12:27
  • 1
    @Will - which is hilarious. "Do as we say, not as we do" - MS – cjk Apr 27 '09 at 12:55
  • 1
    That's not hungarian notation, [since it does not indicate the variable's type or use](https://msdn.microsoft.com/en-us/library/bb164506.aspx), but it being a class member. There are good reasons for the m_ prefix and many people recommend it (eg. [Steve McConnell](http://cc2e.com/Page.aspx?hid=225) - "Does the convention distinguish among local, class, and global data?"). – Sefe Jan 03 '17 at 11:38
0

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.

Razzie
  • 30,834
  • 11
  • 63
  • 78
0
  • CA1709 : protected is not private and needs to be upper case
  • CA1707 : underscores are not according to ms naming convention
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193