Author - StudySection Post Views - 457 views
c#

C# Coding Standards and Best Practices

I am going to describe here several rules and conventions which are useful for developing applications and class libraries in C#. Here, the main goal is to define guidelines to enforce the consistent style and formatting to help developers avoid common mistakes while coding in C#.

Naming Conventions

General Guidelines

  1. Always use Camel Case or Pascal Case names for naming conventions.
  2. Avoid ALL CAPS and all lowercase names. Although single lowercase words or letters are acceptable.
  3. Do not use names that begin with a numeric character.
  4. Always choose meaningful and specific names.
  5. Do not use abbreviations unless the full name is enormous.
  6. Avoid including the parent class name within a property name.
    Example:
    Customer.Name //Good
    Customer.CustomerName //Bad
  7. It is better to prefix Boolean variables and properties with “Can”, “Is” or “Has” like “CanPopulate”, “IsValid” or “HasContent”, etc.
  8. Do not use Hungarian Notation!
    Example: strName or iCount
  9. Any Abbreviations must be widely known and accepted and avoid abbreviations longer than 5 characters.
  10. For two-letter abbreviations, use uppercase and Pascal Case for longer abbreviations.
  11. Do not use C# reserved words as names.
  12. Avoid adding redundant or meaningless prefixes and suffixes to identifiers
    Example:
    // Bad!
    public enum ColorsEnum {…}
    public class CVehicle {…}
    public struct RectangleStruct {…}

Name Usage & Syntax

  • Class or Struct
    Pascal Case
    Examples:
    private class MyClass
    {…}

    For class name, use either a noun or noun phrase
    For Example:
    private struct ApplicationSettings
    {…}

    When sub-classing another type, add an appropriate class-suffix when possible.
    Examples:
    internal class SpecializedAttribute : Attribute
    {…}
    public class CustomerCollection : CollectionBase
    {…}
    public class CustomEventArgs : EventArgs
    {…}

  • Interface
    Use Pascal Case convention.
    Always prefix the interface name with a capital “I”.
    Example:
    interface ICustomer
    {…}
  • Method
    Pascal Case.
    Try to use a Verb or Verb-Object pair.
    Example:
    public void Execute()
    {…}
    private string GetAssemblyVersion(Assembly target)
    {…}
  • Property
    Pascal Case.
    Never prefix property names with “Get” or “Set”.
    Example:
    public string Name
    {
    get{…}
    Set{…}
    }
  • Field (Public, Protected, or Internal)
    Pascal Case.
    Avoid using non-private Fields! Use Properties instead.
    Example:
    public string Name;
    protected IList InnerList
  • Field (Private)
    Camel Case and prefix with a one underscore (_) character.
    Example:
    private string _name;
  • Variable
    • Camel Case.
    • Avoid using single characters like “x” or “y” except in certain loops like FOR, Foreach, etc.
    • Avoid using variable names like text1, text2, text3, etc.
  • Parameter
    Camel Case.
    Example:
    public void Execute(string commandText, int iterations)
    {…}

Coding Style

Formatting

  1. Never declare more than 1 namespace per file.
  2. Avoid putting multiple classes in a single file.
  3. Declare each variable independently – not within the same statement.
  4. Place namespace “using” statements together at the highest of the file. Group .NET namespaces above custom namespaces.
  5. Group internal class implementation by type within the following order:

    • Member variables.
    • Constructors & Finalizers.
    • Nested Enums, Structs, and Classes.
    • Properties
    • Methods
  6. The sequence of declarations within the type groups based upon access modifier and visibility:
    • Public
    • Protected
    • Internal
    • Private
  7. Segregate interface Implementation by using #region statements.
  8. Always try to use curly braces i.e. { and } in conditional statements.
  9. Always use a Tab & Indention size of 4 to separate and organize code.
  10. Recursively indent all code blocks contained within braces.

Code Commenting

  1. Use // or /// and avoid /*……. */
  2. Place the comment above the code in a separate line, but not just after the end of the code in the same line.
  3. Begin comment text with an uppercase letter and End comment text with a period.
  4. Insert one space between the comment delimiter (//) and therefore the comment text, as shown within the following example.
    Example:
    // The following declaration will create a query to map the fields.
    // It will not run this query.
  5. Do not “flowerbox” comment blocks.
    Example:
    // ***************************************
    // Comment block
    // ***************************************
  6. Use inline-comments to elucidate assumptions, known issues, and algorithm insights.
  7. Do not use inline-comments to explain obvious code. Well written code is self-documenting.
  8. Only use comments for bad code to mention “fix this code” – otherwise remove, or rewrite the code!
  9. Include comments using Task-List keyword flags to permit comment-filtering.
    Example:
    // TODO: Place Database Code Here
    // UNDONE: Removed P\Invoke Call due to errors
    // HACK: Temporary fix until able to refactor
  10. Only use C# comment-blocks for documenting the API.
  11. Always include comments.

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri