中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 其他
C#编码规范和编程好习惯(2)
作者:佚名 时间:2007-09-04 15:33 出处:csdn 责编:月夜寒箫
              摘要:C#编码规范和编程好习惯(2)

花括弧需独立一行,而不象if, for 等可以跟括号在同一行。

好:

            
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++)
            {
            //
            }
            }

良好的编程习惯

遵从以下良好的习惯以写出好程序

            
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

}    

避免使用大文件。如果一个文件里的代码超过300~400行,必须考虑将代码分开到不同类中。

避免写太长的方法。一个典型的方法代码在1~25行之间。如果一个方法发代码超过25行,应该考虑将其分解为不同的方法。

方法名需能看出它作什么。别使用会引起误解的名字。如果名字一目了然,就无需用文档来解释方法的功能了。

好:

            
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.
            // ...
            }

使用C# 或 VB.NET的特有类型,而不是System命名空间中定义的别名类型。

好:

            
int age; 

string name; 

object contactInfo;

不好:

            
Int16 age; String name; Object contactInfo;

别在程序中使用固定数值,用常量代替。

别用字符串常数。用资源文件。

避免使用很多成员变量。声明局部变量,并传递给方法。不要在方法间共享成员变量。如果在几个方法间共享一个成员变量,那就很难知道是哪个方法在什么时候修改了它的值。

必要时使用enum 。别用数字或字符串来指示离散值。

好:

            
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。

不在代码中使用具体的路径和驱动器名。 使用相对路径,并使路径可编程。

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有