latest Post

Coding Standards and Guidelines: Naming Conversions and Style in C#

C# Coding Standards and Guidelines: Naming Conversions and Style

1) Use Pascal casing for type and method and constants.

public class SomeClass
{
const int DefaultSize= 100;
public SomeMethod ()
{}
}

2) Use camel casing for local variable names and method arguments.

public class SomeClass
{
int number;
void MyMethod (int someNumber)
{}

}

3) Prefix member variables with m_. Use Pascal casing for the rest of a member variable name following the m_.

public class SomeClass
{
private int m_Number;
}

4) Name methods using verb-object pair, such as ShowDialog ().

5) Method with return values should have a name describing the value returned, such as GetObjectState ().


6) Use descriptive variable names

    Avoid single character variable names, such as i and t. Instead use index or temp.
    Do not abbreviate words. (such as num instead of number)

7) All member variables should be declared at the top, with one line separating them from the properties or methods.

public class MyClass
{
int m_Number;
string m_Name;
public void SomeMethod1()
{ }
public void SomeMethod2()
{ }
}

8) Declare a local variable as close as possible to its first use.

9) Name interfaces with I prefix

interface IMyInterface
{..}


10) With the exception of zero or one, never hard-code a numeric value; always declare a constant instead.

11) Use the const directive only on natural constants such as the number of days of the week.

12) Avoid using const on read-only variables. For that, use the readonly directive.

public class MyClass
{
public const int DaysInWeek = 7;
public readonly int Number;

public MyClass(int someValue)
{
Number = someValue;
}
}

13) Always use C# predefined types rather than the aliases in the System Namespace.

object NOT Object.
string NOT String.
int NOT Int32.

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

3 comments: