Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C# Naming Conventions and Coding Standards

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 197
    Comment on it

    "C# Naming Conventions and Coding Standards"

    Naming Convention:

        Basicaly three types of naming conventions are used in C#, Camel, Pascal and Hungarian.

    We should use PascalCasing for class names and method names:

    Example:

    public class ClientActivity
    {
        public void ClearStatistics()
        {
            //...
        }
    }
    

    We should use camelCasing for method arguments and local variables:

    Example:

    public class UserLog
    {
        public void Add(LogEvent logEvent)
        {
            int itemCount = logEvent.Items.Count;
            // ...
        }
    }
    

    We should not use Hungarian notation as it is no longer used in C#.

    Note:-> Always use proper and meaningful names for declaring variables, method arguments, Classes, etc. for good readability of the code.

    Example:

    // Incorrect
    int iCounter;
    string strName;
    

    Coding Standards:

    1. Do not write constant with capital letters.

    Example:

    // Incorrect
    public static const double PIE = 3.14;
    

    2. Avoid using abbreviations.

    Example:

    // Incorrect
    UserGroup usrGrp;
    

    3. Avoid using Underscores in declaring identifiers.

    Example:

    // Incorrect
    public DateTime client_Appointment;
    

    We can use Underscore in declaring private static variables.

    4. Prefix I with the name of the interfaces.

    Example:

    public interface IShape
    {
    }
    

    5. Use proper indentation. (Ctrl+K+D) is a shortcut key to provide indentation in Visual Studio.

    Example:

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    

    6. Declare static variables at top of the class.

    Example:

    public class Account
    {
        public static string BankName;
        public static decimal Reserves;
    
        public string Number {get; set;}
    
    
        // Constructor
        public Account()
        {
            // ...
        }
    }
    

    7.Use singular names for enum.

    Example:

    public enum Color
    {
        Red,
        Green,
        Blue,
        Yellow,
        Magenta,
        Cyan
    }
    

    8. Donot suffix names of the enum with "Enum".

    Example:

    // Incorrect
    public enum CoinEnum
    {
        Penny,
        Nickel,
        Dime,
        Quarter,
        Dollar
    }
    

    9. Use predefined type names instead of system type names.

    Example:

    //Correct
    int lastIndex;
    
    //Incorrect
    Int32 lastIndex;
    

    10.Always declare namespaces with a clearly defined structure.

    Example:

    namespace Company.Product.Module.SubModule
    

    Happy Coding...!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: