[转帖]C# 编码规范和编程好习惯 |
|---|
123SAY
等级:侠之大者
文章:658
积分:576
注册:2002年8月15日
威望:24 点
|
小 大 楼主 个性首页 QQ 邮箱 主页 发短信 加为好友 |
|
翻译:杨贺宏 谁都会写代码!几个月的编程经验可以让你写出“可运行应用程序”。让它可运行容易,但是以最有效率的方式编码就需要下更多的功夫! 命名惯例和规范注记 : Pascal 大小写形式-所有单词第一个字母大写,其他字母小写。 Camel 大小写形式-除了第一个单词,所有单词第一个字母大写,其他字母小写。 public class HelloWorld{ ...}public class HelloWorld{ void SayHello(string name) { ... }}public class HelloWorld{ int totalCount = 0; void SayHello(string name) { string fullMessage = "Hello " + name; ... }} 以前,多数程序员喜欢它-把数据类型作为变量名的前缀而m_作为成员变量的前缀。例如: string m_sName;int nAge;然而,这种方式在.NET编码规范中是不推荐的。所有变量都用camel 大小写形式,而不是用数据类型和m_来作前缀。 - 别用缩写。用name, address, salary等代替 nam, addr, sal - 别使用单个字母的变量象i, n, x 等. 使用 index, temp等 用于循环迭代的变量例外: for ( int i = 0; i < count; i++ ){ ...}如果变量只用于迭代计数,没有在循环的其他地方出现,许多人还是喜欢用单个字母的变量(i) ,而不是另外取名。 - 变量名中不使用下划线 (_) 。 - 命名空间需按照标准的模式命名
例如,对于类HelloWorld, 相应的文件名应为 helloworld.cs (或, helloworld.vb) 缩进和间隔 bool SayHello (string name) { string fullMessage = "Hello " + name; DateTime currentTime = DateTime.Now; string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString(); MessageBox.Show ( message ); if ( ... ) { // Do something // ... return false; } return true; } 这段代码看起来比上面的好:: bool SayHello ( string name ) { string fullMessage = "Hello " + name; DateTime currentTime = DateTime.Now; 好: if ( ... ) { // Do something }不好: if ( ... ) { // Do something }好: if ( showResult == true ) { for ( int i = 0; i < 10; i++ ) { // } }不好: if(showResult==true) { for(int i= 0;i<10;i++) { // } }以前,多数程序员喜欢它-把数据类型作为变量名的前缀而m_作为成员变量的前缀。例如: string m_sName;int nAge;然而,这种方式在.NET编码规范中是不推荐的。所有变量都用camel 大小写形式,而不是用数据类型和m_来作前缀。 - 别用缩写。用name, address, salary等代替 nam, addr, sal - 别使用单个字母的变量象i, n, x 等. 使用 index, temp等 用于循环迭代的变量例外: for ( int i = 0; i < count; i++ ){ ...}如果变量只用于迭代计数,没有在循环的其他地方出现,许多人还是喜欢用单个字母的变量(i) ,而不是另外取名。 - 变量名中不使用下划线 (_) 。 - 命名空间需按照标准的模式命名
例如,对于类HelloWorld, 相应的文件名应为 helloworld.cs (或, helloworld.vb) 缩进和间隔 bool SayHello (string name) { string fullMessage = "Hello " + name; DateTime currentTime = DateTime.Now; string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString(); MessageBox.Show ( message ); if ( ... ) { // Do something // ... return false; } return true; } 这段代码看起来比上面的好:: bool SayHello ( string name ) { string fullMessage = "Hello " + name; DateTime currentTime = DateTime.Now; 好: if ( ... ) { // Do something }不好: if ( ... ) { // Do something }好: if ( showResult == true ) { for ( int i = 0; i < 10; i++ ) { // } }不好: if(showResult==true) { for(int i= 0;i<10;i++) { // } } |
|
|
|
lkh3331947
等级:论坛游侠
文章:20
积分:256
注册:2005年6月24日
威望:0 点
|
小 大 沙发 个性首页 QQ 邮箱 主页 发短信 加为好友 |
|
[em01] [该帖子由作者于2007年10月16日 13:48:16最后编辑] |
|
|
|
卡通fans
等级:小飞侠
文章:314
积分:1565
注册:2005年8月21日
威望:13 点
|
小 大 板凳 个性首页 QQ 邮箱 主页 发短信 加为好友 |
|
|
|
|
|
jinlicom
等级:侠圣
文章:908
积分:5457
注册:2005年3月16日
威望:0 点
|
小 大 木地板 个性首页 QQ 邮箱 主页 发短信 加为好友 |
|
try: Ctrl+K+D |
|
![]() [img]http://17mybbs.myan.cn/bbs/logo.gif[/img] [url=http://17myb... |
|
|
|
狂轰乱炸
等级:新手上路
文章:2
积分:12
注册:2007年11月23日
威望:0 点
|
小 大 第 5 楼 个性首页 QQ 邮箱 主页 发短信 加为好友 |
|
|
|
|
|
achengheart
等级:新手上路
文章:3
积分:14
注册:2008年5月22日
威望:1 点
|
小 大 第 6 楼 个性首页 QQ 邮箱 主页 发短信 加为好友 |
|
|
|
|
|
|
1
6 | 10
1/1 页
|