C# Coding Standards

 

Some C#.NET coding standards

There are some standards to follow when you start coding in any programming language. Violation of some standards may cause error in the program and violations of some standards do not cause an error in the program but following those standard will make your program look more consistent, professional and readable. Some standards are helpful when your code get into the maintenance phase and when some other programmers work at your code.

C#.NET is a general purpose open-source programming language by Microsoft and follow some coding conventions lets have a look at all those conventions so next time we can take care of those conventions to write even more readable and professional code.

Naming Conventions

Public Naming Conventions

C# highly recommend you to use Pascal Case when naming a class, record or structures, Pascal case means writing the first latter of every word capital (e.g. PascalCase)

//Example of Pascal Case Name of class
public class BussinessModel
{

}

You are suppose to follow the same naming convention when you are making the public property, method or event.

public bool IsValid;

public void MyPascalMethod()
{

}

When it come to making interface classes se I as a prefix in the name of the class e.g.

public class IBusinessModel
{

}

Private naming conventions:

Use camel case when naming a private of some internal fields with the prefix underscore symbol

( _ ) camel case means 2nd word of the variable should start capital letter. camelCase e.g.

public class BusinessModel
{
        private string _valueOne;
}

if the field is private and static as well we will use the prefix s_ and the camel case naming convention.


        private static string s_valueOne;

When writing the parameters of the method use camel case e.g.

public void SomeMethod(int valueOne, int valueTwo)
{

}

Layout Conventions

Complete code should follow these standards

  • Write one statement per line
  • Write only one declaration per line
  • Add at least one blank line between two properties or methods
  • Use parentheses to make clauses in an expression

      if ((val1 > val2) && (val1 > val3))
      {
          // Take appropriate action.
      }

Post a Comment

Previous Post Next Post