Classes

  • DO use PascalCasing.

  • DO name classes with nouns or noun phrases.

    public class Employee
    {
    }
    public class BusinessLocation
    {
    }
    public class DocumentCollection
    {
    }
    

    Bad examples include SearchExamination (a page to search for examinations), Common (does not end with a noun, and does not explain its purpose) and SiteSecurity (although the name is technically okay, it does not say anything about its purpose).

  • CONSIDER ending the name of derived classes with the name of the base class.

Base ClassDerived Class
ValidatorStringValidator NumberValidator RegexValidator
ExceptionIndexOutOfRangeException ArgumentOutOfRangeException

This is very readable and explains the relationship clearly.

  • DO use reasonable judgment in applying this guideline. For example, the Button class is a kind of Control, although Control doesn’t appear in its name.

  • DO use object initializers to simplify object creation.

    // Good:
    var employee = new Employee {
        FirstName = "ABC", LastName = "PQR", Manager = "XYZ", Salary = 12346.25
    };
    
    // Bad:
    var employee = new Employee();
    employee.FirstName = "ABC";
    employee.LastName = "PQR";
    employee.Manager = "XYZ";
    employee.Salary = 12346.25;
    
  • DO NOT include terms like Utility or Helper in classes. Classes with names like that are usually static classes and are introduced without considering object-oriented principles.

  • DO NOT give class names a prefix (e.g., “C”).

    // Avoid
    public class CHelloWorld
    {
        // ...
    }
    
  • DO NOT have number of classes in single file. Create a separate file for each class.

  • AVOID writing long class files. The typical class file should contain 600-700 lines of code. If the class file has more than 700 line of code, you must create partial class. The partial class combines code into single unit after compilation.

Learn More