I am going to be working on a bit of C# code on my own but I want to make sure that I follow the most widely accepted naming conventions in case I want to bring on other dev
In our shop, we started our first C# project using Microsoft's suggested guideline for private members, i.e.
camelCaseFieldName
But we soon ran into confusion between private members and parameters, and switched to
_camelCaseFieldName
which has worked much better for us.
A private member usually has a state that persists outside of a method call - the leading underscore tends to remind you of that.
Also note that using AutoVariable syntax for properties can minimize the need for private backing fields, i.e.
public int PascalCaseFieldName { get; set;}
For a nice concise set of standards that (mostly) follow the MS guidelines, check out net-naming-conventions-and-programming-standards---best-practices
Philips Healtcare C# Coding Standard
MSDN - Eric Gunnerson
Edit: I use "this" keyword to access non-static members in C# and Java.
Have a look at ReSharper. It will underline all the places where your names do not confirm to ordinary guidelines, and you can customize it. Plus, of course there's loads and loads of other productivity enhancements.
We use StyleCop to force consistency throughout our code. StyleCop is used within Microsoft enforce a common set of best practices for layout, readability, maintainability, and documentation of C# source code.
You can run StyleCop at build time and have it generate warnings for style violations.
To answer your specific question, private fields should be in camelCase and prefixed with "this".
private string baseName;
private string prefixName;
private string suffixName;
public GameItem(string _baseName, string _prefixName, string _suffixName)
{
this.baseName = _baseName;
this.prefixName = _prefixName;
this.suffixName = _suffixName;
}
Short answer: use _privateField
, i.e. use leading underscore for private fields.
Long answer: here goes...
Long long ago, Microsoft used to suggest using camelCase
for fields. See here. Note when that document was created, 10/22/2008. Pretty ancient.
Recent code base of Microsoft however depicts a different picture.
We use
_camelCase
for internal and private fields and usereadonly
where possible.
_privateField
.My opinion: I too personally prefer leading underscore for my private fields - makes it very easily distinguishable, without needing the this
.