ASP NET命名规范.docx
《ASP NET命名规范.docx》由会员分享,可在线阅读,更多相关《ASP NET命名规范.docx(16页珍藏版)》请在冰点文库上搜索。
ASPNET命名规范
C#命名规范
MyData就是一个帕斯卡命名的示例
myData是一个骆驼命名法,它第一个单词的第一个字母小写,后面的单词首字母大写,看起来像一个骆驼
iMyData是一个匈牙利命名法,它的小写的i说明了它的型态,后面的和帕斯卡命名相同,指示了该变量的用途.
变量命名规范:
内联变量必须一骆驼camel命名法声明:
forexample:
uName,uPass;
类变量(成员变量)同上,但一定要以一个下划线开始!
forexample:
_mySqlConn
常量命名规范:
带有私有访问修饰的常量必须以骆驼命名法声明,并以一个下划线开始,
forexample:
privateint_SHOW_COUNT=20;
带有公有修饰符.受保护修饰符的常量必须以帕斯卡命名法声明,
forexample:
protectedintCOUNT=20;
数组必须以骆驼命名法声明forexample:
string[5]nameArray;
string[]nameArray=newstring[5];
int[]userAge={20,30,21,22,25,24,22};
结构命名规范:
结构必须以帕斯卡命名法声明,用词或者短语作为声明的首选!
forexample:
publicstructStructName{
//结构体!
}
枚举命名规范:
枚举必须以帕斯卡命名规范命名:
forexample:
publicenumWeekDays{
//结构体
}
=============================================================================
数据类型
数据类型简写
标准命名举例
Array
arr
arrShoppingList
Boolean
bln
blnIsPostBack
Byte
byt
bytPixelValue
Char
chr
chrDelimiter
DateTime
dtm
dtmStartDate
Decimal
dec
decAverageHeight
Double
dbl
dblSizeofUniverse
Integer
int
intRowCounter
Long
lng
lngBillGatesIncome
Object
obj
objReturnValue
Short
shr
shrAverage
Single
sng
sngMaximum
String
str
strFirstName
控件类型
缩写
示例
Animatedbutton
ani
aniMailBox
Button
btn
Checkbox
chk
chkReadOnly
ColorDialog
clrdlg
Combobox,drop-downlistbox
cbo
cboEnglish
Communications
com
comFax
ContextMenu
ctxmnu
Control(usedwithinprocedureswhenthespecifictypeisunknown)
ctr
ctrCurrent
CrystalReportViewer
crvw
Data
dat
datBiblio
Datagrid
dgd
dgdTitles
Datalist
dbl
dblPublisher
Datarepeater
drp
drpLocation
Data-boundcombobox
dbcbo
dbcboLanguage
Data-boundgrid
dbgrd
dbgrdQueryResult
Data-boundlistbox
dblst
dblstJobType
Datetimepicker
dtp
dtpPublished
Directorylistbox
dir
dirSource
DomainUpDown
dupd
Drivelistbox
drv
drvTarget
ErrorProvider
err
Filelistbox
fil
filSource
Flatscrollbar
fsb
fsbMove
FontDialog
fntdlg
Form
frm
frmEntry
Frame
fra
fraLanguage
Gauge
gau
gauStatus
Graph
gra
graRevenue
Grid
grd
grdPrices
GroupBox
grp
HelpProvider
hlp
Hierarchicalflexgrid
flex
flexOrders
HScrollbar
hsb
hsbVolume
Image
img
imgIcon
Imagecombo
imgcbo
imgcboProduct
ImageList
ils
ilsAllIcons
Label
lbl
lblHelpMessage
Line
lin
linVertical
LinkLabel
lnk
Listbox
lst
lstPolicyCodes
ListView
lvw
lvwHeadings
MAPImessage
mpm
mpmSentMessage
MAPIsession
mps
mpsSession
MCI
mci
mciVideo
Menu
mnu
mnuFileOpen
Monthview
mvw
mvwPeriod
MonthCalendar
mcl
MSChart
ch
chSalesbyRegion
MSFlexgrid
msg
msgClients
MSTab
mst
mstFirst
NotifyIcon
nti
NumericUpDown
nupd
OLEcontainer
ole
oleWorksheet
OpenFileDialog
ofdlg
PageSetUpDialog
psdlg
Picturebox
pic
picVGA
Pictureclip
clp
clpToolbar
PrintDocument
prndoc
PrintPreviewControl
ppctl
PrintPreviewDialog
ppdlg
ProgressBar
prg
prgLoadFile
RadioButton
rbtn
RemoteData
rd
rdTitles
RichTextBox
rtf
rtfReport
SaveFileDialog
sfdlg
Shape
shp
shpCircle
Slider
sld
sldScale
Spin
spn
spnPages
Splitter
spt
StatusBar
sta
staDateTime
SysInfo
sys
sysMonitor
TabContrl
tab
TabStrip
tab
tabOptions
Textbox
txt
txtLastName
Timer
tmr
tmrAlarm
Toolbar
tlb
tlbActions
TrackBar
trb
TreeView
tre
treOrganization
UpDown
upd
updDirection
VScrollbar
vsb
vsbRate
1ADO.NET命名规范
数据类型
数据类型简写
标准命名举例
Connection
con
conNorthwind
Command
cmd
cmdReturnProducts
Parameter
parm
parmProductID
DataAdapter
dad
dadProducts
DataReader
dtr
dtrProducts
DataSet
dst
dstNorthWind
DataTable
dtbl
dtblProduct
DataRow
drow
drowRow98
DataColumn
dcol
dcolProductID
DataRelation
drel
drelMasterDetail
DataView
dvw
dvwFilteredProducts
2WinFormControl命名规范
数据类型
数据类型简写
标准命名举例
Label
lbl
lblMessage
LinkLabel
llbl
llblToday
Button
btn
btnSave
TextBox
txt
txtName
MainMenu
mmnu
mmnuFile
CheckBox
chk
chkStock
RadioButton
rbtn
rbtnSelected
GroupBox
gbx
gbxMain
PictureBox
pic
picImage
Panel
pnl
pnlBody
DataGrid
dgrd
dgrdView
ListBox
lst
lstProducts
CheckedListBox
clst
clstChecked
ComboBox
cbo
cboMenu
ListView
lvw
lvwBrowser
TreeView
tvw
tvwType
TabControl
tctl
tctlSelected
DateTimePicker
dtp
dtpStartDate
HscrollBar
hsb
hsbImage
VscrollBar
vsb
vsbImage
Timer
tmr
tmrCount
ImageList
ilst
ilstImage
ToolBar
tlb
tlbManage
StatusBar
stb
stbFootPrint
OpenFileDialog
odlg
odlgFile
SaveFileDialog
sdlg
sdlgSave
FoldBrowserDialog
fbdlg
fgdlgBrowser
FontDialog
fdlg
fdlgFoot
ColorDialog
cdlg
cdlgColor
PrintDialog
pdlg
pdlgPrint
3WebControl命名规范
数据类型
数据类型简写
标准命名举例
AdRotator
adrt
Example
Button
btn
btnSubmit
Calendar
cal
calMettingDates
CheckBox
chk
chkBlue
CheckBoxList
chkl
chklFavColors
CompareValidator
valc
valcValidAge
CustomValidator
valx
valxDBCheck
DataGrid
dgrd
dgrdTitles
DataList
dlst
dlstTitles
DropDownList
drop
dropCountries
HyperLink
lnk
lnkDetails
Image
img
imgAuntBetty
ImageButton
ibtn
ibtnSubmit
Label
lbl
lblResults
LinkButton
lbtn
lbtnSubmit
ListBox
lst
lstCountries
Panel
pnl
pnlForm2
PlaceHolder
plh
plhFormContents
RadioButton
rad
radFemale
RadioButtonList
radl
radlGender
RangeValidator
valg
valgAge
RegularExpression
vale
valeEmail_Validator
Repeater
rpt
rptQueryResults
RequiredFieldValidator
valr
valrFirstName
Table
tbl
tblCountryCodes
TableCell
tblc
tblcGermany
TableRow
tblr
tblrCountry
TextBox
txt
txtFirstName
ValidationSummary
vals
valsFormErrors
XML
xmlc
xmlcTransformResults
======================================================================
part2.
C#CodingStandard
一、命名
1.用pascal规则来命名方法和类型.
publicclassTextBox
{
publicvoidDataBind()
{
}
}
2.用camel规则来命名局部变量和方法的参数.
stringuserName;
publicAddUser(stringuserId,byte[]password);
3.所有的成员变量前加前缀m_
publicclassDatabase
{
publicstringm_connectionString;
}
4.接口的名称加前缀I.
interfaceICompare
{
intcompare();
}
5.自定义的属性以Attribute结尾
publicclassAuthorAttribute:
Attribute
{
}
6.自定义的异常以Exception结尾
publicclassAppException:
Exception
{
}
7.方法的命名.一般将其命名为动宾短语.
ShowDialog()
CreateFile()
GetPath()
8.代码的缩进.要用Tab,而不要用space.
9.局部变量的名称要有意义.不要用x,y,z等等.
stringuserName
10.所有的成员变量声明在类的顶端,用一个换行把它和方法分开.
11.用有意义的名字命名namespace,如:
产品名、公司名.
12.建议局部变量在最接近使用它时再声明.
13.使用某个控件的值时,尽量命名局部变量.
14.把引用的系统的namespace和自定义或第三方的分开.
15.文件名要能反应类的内容,最好是和类同名,一个文件中一个类.
16.目录结构中要反应出namespace的层次.
17.大括号"{"要新起一行.
publicclassAuthorAttribute:
Attribute
{
}
二、编码习惯.
1.用C#预定义的类名,而不要用别名.
stringuserName;而不是System.StringuserName;
intnumber;而不是System.Int32;
2.一行不要超过80个字符.
3.尽量不要手工更改机器生成的代码,若必须更改,一定要改成和机器生成的代码风格一样.
4.关键的语句(包括声明关键的变量)必须要写注释.
5.文字常量和数字常量不要硬编码,应该用常量类或枚举代替.
6.不要用goto系列语句.
7.不要声明public和protected的成员变量,应用property.
8.不要声明public的event,应用事件访问器.
publicclassSource
{
privateEventHandlerm_NumberChangeEvent;
publiceventEventHandlerNumberChangeEvent
{
add
{
m_NumberChangeEvent+=value;
}
remove
{
m_NumberChangeEvent-=value;
}
}
}
9.类型转换的使用规则.
Animalanimal=newDog();
Dogdog=animalasDog;
if(dog!
=null)
{
}
10.生成和构建一个长的字符串时,一定要使用StringBuilder,而不用string.
11.始终使用"{}"包含if下的语句,即使只有一条语句.
12.switch语句一定要有default来处理意外情况.
13.尽量不要使用三目运算符?
:
,而要使用if语句.
14.尽量不用使用this引用,除非是要调用类中的另一个Constructor.
publicclassPerson
{
publicPerson(stringname)
{
}
publicPerson():
this("Jim")
{
}
}