Pascal Casing - The first character of all words is uppercase, the rest are lowercase. For example
PascalCase.
Camel Casing - The first character of all words except the first word is uppercase, all other characters including the first character of the first word are lowercase. For example
camelCase.
public class EntityClass
{
private string entityProperty;
public EntityMethod (string value)
{
...
}
public string EntityProperty
{
get
{
return this.entityProperty;
}
set
{
this.entityProperty = value;
}
}
}
Pascal case for class names, method names and property names.
Camel case for variables and method parameters.
It is not considered good practice to prefix variables with the type of variable (e.g. strName) like you would in Visual Basic for example. Also it is also not considered good practice to use m_ for member variables (although since VB.NET is not a case sensitive language, you will still see this quite frequently).
An exception to this is when defining an interface, in which case the interface name should be prefixed with a capital I (e.g. ISortable).
You should use meaningful names for variables and methods, also methods should be specific in what they do and only perform one function.
Class names and enumerations should be singular (e.g. class Customer, or enum Protocol), collections should be plural (e.g. a List<Customer> should be called Customers)
You will notice that various Microsoft classes do not even conform to these standards and some of the code samples you see online do not either. However it is a good habit to get into, especially if you are part of a development team as all the source code should be "typed, cased & formatted" the same.
Code should be formatted like this:
public string EntityProperty
{
get
{
return this.entityProperty;
}
}
Using Tabs to indent stepped lines, with braces in line with the code they belong to (in the sample above, the opening brace after the
get keyword is on the line below in line with the "g".
Personally I also do not have a problem with using
get { return this.entityProperty; } in accessors where the getter or setter is a single line as it keeps the length of the code page shorter.
I always refer to variables according to their position within the class. e.g. a private string called name in a class called Person should be referred to as
this.name. This can help make code more readable as you know that certain variables are class members as opposed to variables that are used in the scope of a method.
You should also use
base when referencing a property on the base class in a particular instance.
This article was published on the 8th March 2008