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
Personally, I hack the parameter names by the prefix "the" such as theSamplingRate. For me, it makes perfect sense :)
As it was mentioned, Microsoft Naming Guidelines dose not cover private fields and local variable naming. And you don't find consistency within Microsoft itself. If you generate class or Disposable pattern in Visual Studio it will create something like
public MyClass(int value)
{
this.value = value;
}
or
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
...
}
}
Fortunately more and more code was opened by Microsoft, so let's take a look a their repos, e.g. ASP.NET Core MVC
private readonly IControllerActivator _controllerActivator;
private readonly IControllerPropertyActivator[] _propertyActivators;
Or .NET Core
private T[] _array;
You may say, that it's not actually Microsoft, but .NET Foundation. Fair enough, let's take a look at Microsoft repos:
private readonly MetricSeries zeroDimSeries;
But here is ancient Microsoft implementation of MVC
private IActionInvoker _actionInvoker;
So there is not any common practice or official guideline regarding private fields naming. Just choose one you prefer and stick to it.
I do this; it's pretty much in line with MSDN.
class MyClass : MyBaseClass, IMyInterface
{
public event EventHandler MyEvent;
int m_MyField = 1;
int MyProperty {
get {
return m_MyField;
}
set {
m_MyField = value;
}
}
void MyMethod(int myParameter) {
int _MyLocalVaraible = myParameter;
MyProperty = _MyLocalVaraible;
MyEvent(this, EventArgs.Empty);
}
}
Here's a little more detail: http://jerrytech.blogspot.com/2009/09/simple-c-naming-convention.html
The most important thing is to pick one standard and stick with it. Check out iDesign's C# Coding Standard at IDesign (it's a link on the right side). It's a great document that covers things like naming guidelines. They recommend using camel case for both local variables and method arguments.
Follow the Microsoft Naming Guidelines. The guidelines for field usage indicate that it should be camelCase and not be prefixed. Note that the general rule is no prefix; the specific rule is not to prefix to distinguish between static and non-static fields.
Do not apply a prefix to field names or static field names. Specifically, do not apply a prefix to a field name to distinguish between static and nonstatic fields. For example, applying a g_ or s_ prefix is incorrect.
and (from General Naming Conventions)
Do not use underscores, hyphens, or any other nonalphanumeric characters.
EDIT: I will note that the docs are not specific with regard to private fields but indicate that protected fields should be camelCase only. I suppose you could infer from this that any convention for private fields is acceptable. Certainly public static fields differ from protected (they are capitalized). My personal opinion is that protected/private are not sufficiently different in scope to warrant a difference in naming convention, especially as all you seem to want to do is differentiate them from parameters. That is, if you follow the guidelines for protected fields, you'd have to treat them differently in this respect than private fields in order to distinguish them from parameters. I use this
when referring to class members within the class to make the distinction clear.
EDIT 2
I've adopted the convention used at my current job, which is to prefix private instance variables with an underscore and generally only expose protected instance variables as properties using PascalCase (typically autoproperties). It wasn't my personal preference but it's one that I've become comfortable with and probably will follow until something better comes along.