程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#開發編碼規范

C#開發編碼規范

編輯:關於C#
 

Pascal 大小寫形式——所有單詞第一個字母大寫,其他字母小寫。

Camel 大小寫形式——除了第一個單詞,所有單詞第一個字母大寫,其他字母小寫。

類名使用Pascal大小寫形式

public class HelloWorld

{

}

方法使用Pascal大小寫形式

public class HelloWorld

{

void SayHello(string name)

{

}

}

變量和方法參數使用Camel 大小寫形式

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)

 

縮進和間隔

縮進用TAB。不用 SPACES。注釋需和代碼對齊。花括弧 ( {} ) 需和括號外的代碼對齊。用一個空行來分開代碼的邏輯分組。

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,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++)

{

//

}

}

 

良好的編程習慣

遵從以下良好的習慣以寫出好程序。

避免使用大文件。如果一個文件裡的代碼超過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,別用數字或字符串來指示離散值。  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved