C# Coding Standards/Conventions And Best Practices

 

Below are some of the best practices which all the .Net Developers should follow:

  • Class and Method names should always be in Pascal Case [ The first character of all words is in upper case and other characters are in lower case. Example: HelloWorld ]
    • public class Employee
      {
          public Employee GetDetails()
          {
              //...
          }
          public double GetBonus()
          {
              //...
          }
      }
  • Method argument and Local variables should always be in Camel Case

    public class Employee
    {
        public void PrintDetails(int employeeId, String firstName)
        {
            int totalSalary = 2000;
            // ...
        }
    }
  • Avoid the use of underscore while naming identifiers
    // Correct
    public DateTime fromDate;
    public String firstName;
     
    
    // Avoid
    public DateTime from_Date;
    public String first_Name;
  • Avoid the use of System data types and prefer using the Predefined data types.

    // Correct
    int employeeId;
    string employeeName;
    bool isActive;
     
    
    // Avoid
    Int32 employeeId;
    String employeeName;
    Boolean isActive;
  • Always prefix an interface with letter I.
  • Always use the using keyword when working with disposable types. It automatically disposes the object when program flow leaves the scope.

    using(var conn = new SqlConnection(connectionString))
    {
        // use the connection and the stream
        using (var dr = cmd.ExecuteReader())
        {
         //
        }
    }
  • Always declare the variables as close as possible to their use.

    // Correct
    String firstName = "Shubham";
    Console.WriteLine(firstName);
    //--------------------------
     
    
    // Avoid
    String firstName = "Shubham";
    //--------------------------
    //--------------------------
    //--------------------------
    Console.WriteLine(firstName);
  •  Always declare the properties as private so as to achieve Encapsulation and ensure data hiding.

    // Correct
    private int employeeId { get; set; }
    
    // Avoid
    public int employeeId { get; set; }
  • Constants should always be declared in UPPER_CASE.

    // Correct
    public const int MIN_AGE = 18;
    public const int MAX_AGE = 60;
    
    // Avoid
    public const int Min_Age = 18;
    public const int Max_Age = 60;
  • Use // or /// for comments avoid using /* … *
  • Dont have number of classes in single file. Create a separate file for each class.
  • Avoid the use of var in place of dynamic.
  • Do not hardcode string or numbers; 
    instead, create separate file sfor constants and put all constants into that or declare constants on top of file 
    and refer these constants into your code.
  • Use String.Empty instead of “”
  • While working with the collection be aware of the below points:
  1. While returning collection return empty collection instead of returning null when you have no data to return.
  2. Always check Any() operator instead of checking count i.e. collection.Count > 0 and checking of null
  3. Use foreach instead of for loop while traversing.
  4. Use IList<T>, IEnumrable<T>,ICollection<T> instead of concrete classes e.g. using List<>

Architecture Design Level Guidelines

  • Do not access the database from UI pages. Use the data access layer to perform all tasks which are related to the database.
  • Try to use design patterns, practices, and SOLID principles.
  • Always refer to minified versions of javascript or CSS files, this will reduce unnecessary overhead to the server
  • Use Nullable Data Types Whenever Required

    int index = 0; // simple declaration of int
    int? index = null; // nullable data type declaration

Prefer “is” and “as” Operators While Casting

  • Use Conditional Attributes When You Need Them


 



Comments