Besides the two you mention, it is very common in C# to not have a prefix for private members.
class Foo
{
private int i;
private string id;
}
That is what I use, and also what is recommended in Microsoft's internal naming guidelines.
See also these .NET naming guidelines.
As noted by Brad Abrams: internal coding guidelines:
Do not use a prefix for member variables (
_
,m_
,s_
, etc.). If you want to distinguish between local and member variables you should use “this.
” in C# and “Me.
” in VB.NET.
I think the important principle here is that you're consistent. Use a "_" or "m" prefix if that's what you like to do and it's consistent with the rest of the code you're working with. Whatever you choose, stick with it and be consistent.
I prefer to use your first example, or auto-properties like this, which avoid having the private fields defined in your class at all:
public String MyString { get; set; }
Use the prop
snippet to make these REALLY fast.
As I recall from reading Framework Design Guidelines, there really is no set convention for private member variables, except that one should not use Hungarian notation and one should not capitalize the first letter of the variable name (use Camel-casing). There are quotes in the book that support both of your examples, as well as not using any prefix at all.
Personally, I prefer the "m_" prefix.
General guidance from Microsoft here:
http://msdn.microsoft.com/en-us/library/ms229002.aspx
Automatic properties in C# are great and I uses then when I can but there are cases where they don't work for me such as when doing type or value checking on a set method.
In general: use camel casing and don't prefix your name with anything such as underscore or a type prefix.
public int Age {get; set;}
or
private int age;
public int Age
{
get { return age; }
set
{
if(value < 0)
throw new InvalidOperationException("Age > 0");
age = value;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With