What naming conventions should I use in naming variables, classes, Namespaces, controls, etc. Should I put C as the first letter of every class? Do I prefix variables, for example s or
str for strings?
There is no need to use Hungarian notation like strName for string variables because when you hover mouse on the variable Intellisense automatically shows its data type.
Pascal case is used in .NET for naming Namespaces, Classes, Methods, Properties etc.
Examples:
Namespaces:
Use company name then technology name and then feature name with Pascal case like XYZCompany.Media
Classes:
Use noun or noun phrase with Pascal case like ImageViewer.
Don't use C as prefix for classes.
Interfaces:
Use I as prefix with Pascal case like IViewer.
Variables:
Either use Camel case like userID or Pascal case like UserID
Controls:
Use 3 digit prefix with Camel case.
Textbox: txtName
Button: btnName
Label: lblName
Panel: pnlViewer
Image: imgName
Properties:
Use noun or noun phrase with Pascal case like UserID, Password etc.
Methods:
Use verb or verb phrase with Pascal case like InsertQuery(), GetUserInfo() etc.
Please Mark as Answer if this post helps you.
Thanks
Marked as answer by tgleason on May 27, 2010 03:52 PM
I did find that article "Naming Guidelines" before I posted. It is for "Class Library Developers". I thought there might be something more for website/application developers. But it looks like that is all that is out there from Microsoft at least. I'm
not going to bother with Hungarian notation for strings, int, double, etc, or simple types at least.
I have always been hoping to stick to some standard, consistent naming convention. So far I have been following the MSDN recommendations, like what you have laid out below.
Do you have a reference to the naming conventions of UI controls (Controls: Use 3 digit prefix with Camel case)? I think this is essentially Hungarian notation, which is not recommended. Maybe an exception for UI
controls? I am following this practice for UI controls, but I can't find an MSDN article about it.
How about event handler names? Suppose I have a button with ID "btnSubmit", What name should I use for its click event handler? I have been using "btnSubmit_Click", but then it violates the method naming convention in C#, at least, it should
not start with a lower case letter, and also Hungarian notation is not recommended for a method or variable name. Additionally, underscores are not recommended in a method name, but it seems Microsoft has been using underscores for all event handlers (e.g.:
Page_Load).
So, if Hungarian notation is generally recognized/accepted for UI controls (e.g., btnSubmit), what name should we use for event handlers? btnSubmit_Click? btnSubmit_Clicked? SubmitClicked? SubmitClick?SubmitClickEventHandler? SubmitButtonClicked? SubmitButtonClickEventHandler?
There is no need to use Hungarian notation like strName for string variables because when you hover mouse on the variable Intellisense automatically shows its data type.
Pascal case is used in .NET for naming Namespaces, Classes, Methods, Properties etc.
Examples:
Namespaces:
Use company name then technology name and then feature name with Pascal case like XYZCompany.Media
Classes:
Use noun or noun phrase with Pascal case like ImageViewer.
Don't use C as prefix for classes.
Interfaces:
Use I as prefix with Pascal case like IViewer.
Variables:
Either use Camel case like userID or Pascal case like UserID
Controls:
Use 3 digit prefix with Camel case.
Textbox: txtName
Button: btnName
Label: lblName
Panel: pnlViewer
Image: imgName
Properties:
Use noun or noun phrase with Pascal case like UserID, Password etc.
Methods:
Use verb or verb phrase with Pascal case like InsertQuery(), GetUserInfo() etc.
Yes there is an exception for UI controls.
MS left it on developers, it's your free will to name any control in your own style but it should make sense.
You may use either CamelCase/Hungarian notation or PascalCase but it should be consistent throughout your project.
The following examples are not standard but you may use them:
tgleason
Member
13 Points
24 Posts
ASP.NET(C#) Naming Conventions
May 26, 2010 05:22 PM|LINK
What naming conventions should I use in naming variables, classes, Namespaces, controls, etc. Should I put C as the first letter of every class? Do I prefix variables, for example s or str for strings?
Does microsoft have a document on this topic?
Thank you
nareshguree2...
Star
11118 Points
1997 Posts
Re: ASP.NET(C#) Naming Conventions
May 26, 2010 05:28 PM|LINK
you can use ISO standard Naming convention or CMM level cnvention.
like Function name "Add"
variable name "strQuery"
Control Name "btnSubmit"
Class Name "Common"
Namespace "Sanjog.Web"
public Property Name "UniqueId"
private variable "_uniqueId"
lspence
Star
11493 Points
1707 Posts
Re: ASP.NET(C#) Naming Conventions
May 26, 2010 05:52 PM|LINK
Refer to the following link.
http://Lspence.blogspot.com
(Please MARK this post as ANSWERED, if you find it helpful)
kedarrkulkar...
All-Star
34177 Points
5490 Posts
Re: ASP.NET(C#) Naming Conventions
May 26, 2010 06:04 PM|LINK
MSDN naming guidelines..
http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
ASP .NET COD...
Member
28 Points
10 Posts
Re: ASP.NET(C#) Naming Conventions
May 26, 2010 06:43 PM|LINK
Microsoft provides Naming Guidelines on this URL:
http://msdn.microsoft.com/en-us/library/xzf533w0(VS.71).aspx
There is no need to use Hungarian notation like strName for string variables because when you hover mouse on the variable Intellisense automatically shows its data type.
Pascal case is used in .NET for naming Namespaces, Classes, Methods, Properties etc.
Examples:
Namespaces:
Use company name then technology name and then feature name with Pascal case like XYZCompany.Media
Classes:
Use noun or noun phrase with Pascal case like ImageViewer.
Don't use C as prefix for classes.
Interfaces:
Use I as prefix with Pascal case like IViewer.
Variables:
Either use Camel case like userID or Pascal case like UserID
Controls:
Use 3 digit prefix with Camel case.
Textbox: txtName
Button: btnName
Label: lblName
Panel: pnlViewer
Image: imgName
Properties:
Use noun or noun phrase with Pascal case like UserID, Password etc.
Methods:
Use verb or verb phrase with Pascal case like InsertQuery(), GetUserInfo() etc.
Thanks
tgleason
Member
13 Points
24 Posts
Re: ASP.NET(C#) Naming Conventions
May 27, 2010 04:15 PM|LINK
Hi, Thanks for responding.
I did find that article "Naming Guidelines" before I posted. It is for "Class Library Developers". I thought there might be something more for website/application developers. But it looks like that is all that is out there from Microsoft at least. I'm not going to bother with Hungarian notation for strings, int, double, etc, or simple types at least.
Thanks again.
antonyliu200...
Member
168 Points
310 Posts
Re: ASP.NET(C#) Naming Conventions
Dec 01, 2010 04:18 PM|LINK
I have always been hoping to stick to some standard, consistent naming convention. So far I have been following the MSDN recommendations, like what you have laid out below.
Do you have a reference to the naming conventions of UI controls (Controls: Use 3 digit prefix with Camel case)? I think this is essentially Hungarian notation, which is not recommended. Maybe an exception for UI controls? I am following this practice for UI controls, but I can't find an MSDN article about it.
How about event handler names? Suppose I have a button with ID "btnSubmit", What name should I use for its click event handler? I have been using "btnSubmit_Click", but then it violates the method naming convention in C#, at least, it should not start with a lower case letter, and also Hungarian notation is not recommended for a method or variable name. Additionally, underscores are not recommended in a method name, but it seems Microsoft has been using underscores for all event handlers (e.g.: Page_Load).
So, if Hungarian notation is generally recognized/accepted for UI controls (e.g., btnSubmit), what name should we use for event handlers? btnSubmit_Click? btnSubmit_Clicked? SubmitClicked? SubmitClick?SubmitClickEventHandler? SubmitButtonClicked? SubmitButtonClickEventHandler?
Please share your two cents. Thank you very much.
ASP .NET COD...
Member
28 Points
10 Posts
Re: ASP.NET(C#) Naming Conventions
Dec 05, 2010 12:18 PM|LINK
Yes there is an exception for UI controls.
MS left it on developers, it's your free will to name any control in your own style but it should make sense.
You may use either CamelCase/Hungarian notation or PascalCase but it should be consistent throughout your project.
The following examples are not standard but you may use them:
LinkButton: lnkBtn
ImageButton: imgBtn
Hyperlink: hyp
ComboBox: cmb
DropDownList: ddl
ListBox: lst
CheckBox: chk
CheckBoxList: chkLst
RadioButton: rdo
RadioButtonList:rdoLst
Image: img
ImageMap: imgMap
Table: tbl
HiddenField: hdn
Literal: lrl
Panel: pnl
PlaceHolder: plh
Menu: mnu
Repeater: rpt
Timer: tmr
UpdatePanel: updPnl
For event-handlers you may follow MS approach:
ControlName_EventName like btnSubmit_Click.
Thanks