
Quick Tips for Following Csharp Coding Standards and Naming Conventions
Originally posted 12 Years Ago
Today, I am going to explain C# coding standards naming conventionsĀ for best practice when you are developing a 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 First letter of every word is in capital letter.
Underscore PrefixĀ (_underScore) : For underscore ( __ ), the word after _ use camelCase terminology.
Native DataType
Always use native datatype instead of .Net CTS type. For exampleĀ useĀ intĀ instead ofĀ Int32Ā orĀ Int64.
//Good
private int _salary = 100;
//Bad
private Int16 _salary = 100;
private Int32 _salary=100;
Class
Always useĀ PascalCaseĀ for class names. Try to use noun or noun phrase for class name.
public partial class About : Page
{
//...
}
Methods
Always useĀ PascalCaseĀ for method names.Ā Use maximum 7 parameter for a method.
public string GetPosts(string postId)
{
//...
}
NOTE:Ā Donāt use name as all character in CAPS.
Arguments and Local Variable
Always useĀ camelCaseĀ with methodās arguments and local variables. Donāt use Hungarian notation for variables.
public string GetPosts(string postId
{
int numberOfPost = 0;
}
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.
private int _salary = 100;
public int Salary
{
get
{
return _salary;
}
set
{
_salary = value;
}
}
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.
public interface IUser
{
/// <summary>
/// Check user is exists or not
/// </summary>
/// <returns>return bool value</returns>
bool ValidateUser();
}
Private member variable
Always tried to useĀ camelCaseĀ terminology prefix with underscoreĀ ( _ )
private int _salary = 100;
Public Member Varibale
Always useĀ PascalCaseĀ for public member variable
public int Salary = 100;
Member variable
Declare member variable at theĀ topĀ on the class, If class hasĀ staticĀ member than it will come at theĀ top mostĀ and after that other member variable
public class Account
{
public static string BankName;
public static decimal Reserves;
public string Number {get; set;}
public DateTime DateOpened {get; set;}
public DateTime DateClosed {get; set;}
public decimal Balance {get; set;}
// Constructor
public Account()
{
// ...
}
}
Enum
Always useĀ singular nounĀ to define enum.
enum MailType
{
Html,
PlainText,
Attachment
}
Namespace
Always useĀ PascalCaseĀ for namespace.
namespace NextProgramming.Domain
Standard Abbrevieation 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
Conclusion:
Today We learned Coding standard naming conventions in C#.
Reference:
IĀ hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post.Ā If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks.