.NET Daily

C#

C# Coding Standards Series – Naming Conventions

Posted on .

C# Coding Standards Series – Naming Conventions

Introduction

This post is part of a series that covers most of notable C# coding standards that you should follow.

You can download the document with all of the post from here: [download id=”2″ format=”1″]

You can see here the other posts within the series.


Terminology and Definitions for Naming Conventions:

Camel Case (camelCase): The first letter of the word is lower case and then each first letter of the part of the word is upper case;

Pascal Case (PascalCase): The first letter of the word is upper case and then each first letter of the part of the word is upper case;

Underscode Prefix (_underscore): The word begins with underscore char and for the rest of the word use camelCase rule;

General Rules

1. Use Pascal Case for Class names:

Ex:

2. Use Pascal Case for Method names:

Ex:

3. Use Camel Case for variables and method parameters:

Ex:

4. Avoid all upper case or all lower case names;

5. Do not use Hungarian notation:

Example of Hungarian Notation:

string m_sName; (the prefix m_ means that is a member variable and s means that is a string data type);

6. Avoid abbreviations longer than 5 characters;

7. Avoid using abbreviations unless the full name is excessive:

Ex:

Good: string address;             Not good: string addr;

8. Use meaningfull, descriptive words for naming variables;

9. Try to prefix Boolean variables with “Can”, “Is” or “Has”;

10. Do not use Underscore Prefix for local variables names;

11. All member variables must use Underscore Prefix so that they can be identified from other local variables names;

12. Avoid naming conflicts with existing .NET Framework namespaces or types;

13. Do not include the parent class name within a property name:

Ex: Good: Customer.Name;               Not good: Customer.CustomerName;

14. When defining a root namespace, use a Product, a Company or a Developer name as the root:

Ex: NorthwindApplication.Utilities;

15. Use Pascal Case for file names;

16. Method name should tell you what it does;

17. A method should do only “one job”. Do not combine multiple jobs in one method even if those jobs have very few lines of code.

Ex:

 

Naming Conventions for ASP.NET Controls

In general, naming ASP.NET controls is made using Camel Case naming convention, where the prefix of the name is the abbreviation of the control type name.

Abbreviation ASP.NET Control
STANDARD CONTROLS
btn Button
cb CheckBox
cbl CheckBoxList
ddl DropDownList
fu FileUpload
hdn HiddenField
lnk Hyperlink
img Image
ibtn(btn) ImageButton
lbl Label
lbtn(btn) LinkButton
lb ListBox
lit Literal
mv MultiView
pnl Panel
ph PlaceHolder
rb RadioButton
rbl RadioButtonList
tbl Table
txt TextBox
v View
DATA CONTROLS
dtl DataList
dp DataPager
dtv DetailsView
ets EntityDataSource
fv FormView
gv GridView
lds LinqDataSource
lv ListView
ods ObjectDataSource
qe QueryExtender
rpt Repeater
smd SiteMapDataSource
sds SqlDataSource
xds XmlDataSource
VALIDATION CONTROLS
cpv CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
rfv RequiredFieldValidator
vs ValidationSummary

Please share your opinion about this post by commenting below.

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

Comments
  • user

    AUTHOR Mohammad

    Posted on 4:27 pm January 1, 2012.
    Reply

    Hi,
    Thanks so much …
    This article was really helpful !…

    — Wish you the best —

  • user

    AUTHOR Manoj B. Kalla

    Posted on 11:57 am June 21, 2014.
    Reply

    Dear Darius Dumitrescu,

    Good article . . All beginners and seniors should follow naming conventions.

    Very Good. . .

    Regards,
    Manoj B. Kalla

  • user

    AUTHOR smi

    Posted on 2:00 pm August 9, 2014.
    Reply

    Thanks for sharing this

    Regards
    Irfan

  • user

    AUTHOR Sundar

    Posted on 1:44 am March 22, 2017.
    Reply

    Thanks for the article and the PDF. It is so useful. Could you suggest any Tool to validate these Coding Standards automatically by Visual Studio? Can tools like StyleCop or Resharper be used?

    Thanks
    Sundar

  • View Comments (0) ...
    Navigation