C# Coding Standards And Naming Conventions

Today, I am going to explain C# coding standards naming conventions for best practice when you are developing an application.

Terminology

  • Camel Case (camelCase): In this the first letter of word always in small letter and after that each word with capital letter.
  • Pascal Case (PascalCase): In this the first letter of every word is in capital letter.
  • Underscore Prefix (_underScore): For underscore ( __ ), the word after _ use camelCase terminology.

    Terminology

Native DataType

Always use native datatype instead of .NET CTS type. For example, use int instead of Int32 or Int64.

  1. //Good  
  2. private int _salary = 100;  
  3.   
  4. //Bad  
  5. private Int16 _salary = 100;  
  6. private Int32 _salary=100;  
Class

Always use PascalCase for class names. Try to use noun or noun phrase for class name.
  1. public partial class About : Page  
  2. {  
  3.    //...  
  4. }  
Methods

Always use PascalCase for method names. Use maximum 7 parameter for a method.
  1. public string GetPosts(string postId)  
  2. {  
  3.    //...  
  4. }  
Note: Don't use name as all character in CAPS.

Arguments and Local Variable

Always use camelCase with method arguments and local variables. Don't use Hungarian notation for variables.
  1. public string GetPosts(string postId  
  2. {  
  3.    int numberOfPost = 0;   
  4. }  
Note: Don't use abbreviations for any words and don't use underscore ( _ ) in between any name.

Property

Use PascalCase for property. Never use Get and Set as prefix with property name.
  1. private int _salary = 100;  
  2. public int Salary  
  3. {  
  4.     get  
  5.     {  
  6.         return _salary;  
  7.     }  
  8.     set  
  9.     {  
  10.         _salary = value;  
  11.     }  
  12. }  
Note: Don't use name with start with numeric character.

Interface

Always use letter "I" as prefix with name of interface. After letter I, use PascalCase.
  1. public interface IUser  
  2. {  
  3.    /// <summary>  
  4.    /// Check user is exists or not  
  5.    /// </summary>  
  6.    /// <returns>return bool value</returns>  
  7.    bool ValidateUser();  
  8. }  
Private Member Variable

Always try to use camelCase terminology prefix with underscore ( _ ).
  1. private int _salary = 100;  
Public Member Variable

Always use PascalCase for public member variable,
  1. public int Salary = 100;  
Member Varibale

Member variable

Declare member variable at the top of the class, If class has static member then it will come at the top most and after that other member variable.
  1. public class Account  
  2. {  
  3.     public static string BankName;  
  4.     public static decimal Reserves;  
  5.     public string Number  
  6.     {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public DateTime DateOpened  
  11.     {  
  12.         get;  
  13.         set;  
  14.     }  
  15.     public DateTime DateClosed  
  16.     {  
  17.         get;  
  18.         set;  
  19.     }  
  20.     public decimal Balance  
  21.     {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     // Constructor  
  26.     public Account()  
  27.     {  
  28.         // ...  
  29.     }  
  30. }  
Enum

Always use singular noun to define enum.
  1. enum MailType  
  2. {  
  3.    Html,  
  4.    PlainText,  
  5.    Attachment  
  6. }  
Namespace

Always use PascalCase for namespace.

namespace NextProgramming.Domain

Standard Abbreviation for Standard Controls.
Abbreviations Standard Control
btn Button
cb CheckBox
cbl CheckBoxList
ddl DropDownList
fu FileUpload
hdn HiddenField
hlk Hyperlink
img Image
lbl Label
lbtn LinkButton
mv MultiView
pnl Panel
txt TextBox
DataGrid dtg
imb ImageButton
lst ListBox
dtl DataList
rep Repeater
rdo RadioButton
rdl RadioButtonList
phd Placeholder
tbl Table
gv GridView
dtv DetailView
fv FormView

Today we learned coding standard naming conventions in C#. Thanks for reading this article, hope you enjoyed it.

Up Next
    Ebook Download
    View all
    Learn
    View all