C # - using auto-implemented properties and naming conventions
When using automatic properties such as
public string MyProperty {get; set; }
This is great until you come up with naming conventions.
I am using underscore for class level fields i.e.
string _MyProperty;
so with auto-injection means it is not clear what the variable is and its scope.
If you get my point, any thoughts ??
Malcolm
Edit: Since the property is public, you also don't want to use the underscore character.
source to share
PascalCasing tells you its Public class level. (Take a look at the article on naming conventions for .NET )
source to share
you don't need _ when you are using automatic properties and modern IDEs (like visual studio). intellisense will work correctly if you use automatic properties and various features. the compiler will check it as well.
public string SomeString { private set; get; }
private string some2string;
public string Some3string { set; get; }
this means that SomeString is only an internal record. you won't get the property outside of the class. so there is no magic _ etc.
if you really have a property of the class then there is no need to create the property, just enter the field.
source to share
The property itself is publicly available. The compiler creates private fields for your property. Because auto-implemented properties are not supported by MSIL. But since the private fields are generated by the compiler, you never have to deal with them directly.
An excellent book on naming conventions is the Frame Design Guide 2nd Edition by Brad Abrams. link
A great tool for keeping naming conventions up is Stylecop
field / property:
If you just use "get, set" as in my example, you are correct what the difference should be, but maybe you do an extra task if someone calls a property or sets a property. perhaps you will do some checking on setting a property or updating some internal state of an object.
source to share