- 讲师:刘萍萍 / 谢楠
- 课时:160h
- 价格 4580 元
特色双名师解密新课程高频考点,送国家电网教材讲义,助力一次通关
配套通关班送国网在线题库一套
命名惯例和规范
注记: 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; ... } }
string m_sName; int nAge;然而,这种方式在.NET编码规范中是不推荐的。所有变量都用camel 大小写形式,而不是用数据类型和m_来作前缀。
for ( int i = 0; i < count; i++ ) { ... }如果变量只用于迭代计数,没有在循环的其他地方出现,许多人还是喜欢用单个字母的变量(i) ,而不是另外取名。
. . .
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;
string message = fullMessage + ",
the time is: " + currentTime.ToShortTimeString();
MessageBox.Show ( message );
if ( ... ) { // Do something // ...
return false; }
return true; }
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++) { // } }
良好的编程习惯
遵从以下良好的习惯以写出好程序
void SavePhoneNumber ( string phoneNumber ) { // Save the phone number. }
// This method will save the phone number. void SaveData ( string phoneNumber ) { // Save the phone number. }
// Save the address. SaveAddress ( address ); // Send an email to the supervisor to inform that the address is updated. SendEmail ( address, email ); void SaveAddress ( string address ) { // Save the address. // ... } void SendEmail ( string address, string email ) { // Send an email to inform the supervisor that the address is changed. // ... }
// Save address and send an email to the
supervisor to inform that the address is updated. SaveAddress ( address, email ); void SaveAddress ( string address, string email ) { // Job 1. // Save the address. // ... // Job 2. // Send an email to inform the supervisor that the address is changed. // ... }
int age; string name; object contactInfo;
Int16 age; String name; Object contactInfo;
enum MailType { Html, PlainText, Attachment } void SendMail (string message, MailType mailType) { switch ( mailType ) { case MailType.Html: // Do something break; case MailType.PlainText: // Do something break; case MailType.Attachment: // Do something break; default: // Do something break; } }不好:
void SendMail (string message, string mailType) { switch ( mailType ) { case "Html": // Do something break; case "PlainText": // Do something break; case "Attachment": // Do something break; default: // Do something break; } }
别把成员变量声明为 public 或 protected。都声明为 private 而使用 public/protected 的Properties.
void ReadFromFile ( string fileName ) { try { // read from file. } catch (FileIOException ex) { // log error. // re-throw exception depending on your case. throw; } }不好:
void ReadFromFile ( string fileName ) { try { // read from file. } catch (Exception ex) { // Catching general exception is bad...
we will never know whether it // was a file error or some other error. // Here you are hiding an exception. // In this case no one will ever know that an exception happened. return ""; } }不必在所有方法中捕捉一般异常。不管它,让程序崩溃。这将帮助你在开发周期发现大多数的错误。
原贴: http://tb.blog.csdn.net/TrackBack.aspx?PostId=66467
责编:杨粟梅
上一篇:六种迅速修理C# Bug地办法
课程专业名称 |
讲师 |
课时 |
查看课程 |
---|
课程专业名称 |
讲师 |
课时 |
查看课程 |
---|
点击加载更多评论>>