解析C#編程的通用結(jié)構(gòu)和程序書寫格式規(guī)范
C# 程序的通用結(jié)構(gòu)
C# 程序可由一個或多個文件組成。每個文件都可以包含零個或零個以上的命名空間。一個命名空間除了可包含其他命名空間外,還可包含類、結(jié)構(gòu)、接口、枚舉、委托等類型。以下是 C# 程序的主干,它包含所有這些元素。
// A skeleton of a C# program using System; namespace YourNamespace { class YourClass { } struct YourStruct { } interface IYourInterface { } delegate int YourDelegate(); enum YourEnum { } namespace YourNestedNamespace { struct YourStruct { } } class YourMainClass { static void Main(string[] args) { //Your program starts here... } } }
C# 編碼約定
C# 語言規(guī)范 未定義編碼標(biāo)準。但是,Microsoft 根據(jù)本主題中的準則來開發(fā)樣本和文檔。
編碼約定可實現(xiàn)以下目的:
- 它們?yōu)榇a創(chuàng)建一致的外觀,以確保讀取器專注于內(nèi)容而非布局。
- 它們使得讀取器可以通過基于之前的經(jīng)驗進行的假設(shè)更快地理解代碼。
- 它們便于復(fù)制、更改和維護代碼。
- 它們展示 C# 最佳做法。
命名約定
在不包括 using 指令的短示例中,使用命名空間限定。如果你知道命名空間默認導(dǎo)入項目中,則不必完全限定來自該命名空間的名稱。如果對于單行來說過長,則可以在點 (.) 后中斷限定名稱,如下面的示例所示。
var currentPerformanceCounterCategory = new System.Diagnostics. PerformanceCounterCategory();
你不必更改通過使用 Visual Studio 設(shè)計器工具創(chuàng)建的對象的名稱以使它們適合其他準則。
布局約定
好的布局利用格式設(shè)置來強調(diào)代碼的結(jié)構(gòu)并使代碼更便于閱讀。Microsoft 示例和樣本符合以下約定:
- 使用默認的代碼編輯器設(shè)置(智能縮進、4 字符縮進、制表符保存為空格)。有關(guān)詳細信息,請參閱選項、文本編輯器、C#、格式設(shè)置。
- 每行只寫一條語句。
- 每行只寫一個聲明。
- 如果連續(xù)行未自動縮進,請將它們縮進一個制表符位(四個空格)。
- 在方法定義與屬性定義之間添加至少一個空白行。
使用括號突出表達式中的子句,如下面的代碼所示。
if ((val1 > val2) && (val1 > val3)) { // Take appropriate action. }
注釋約定
將注釋放在單獨的行上,而非代碼行的末尾。
以大寫字母開始注釋文本。
以句點結(jié)束注釋文本。
在注釋分隔符 (//) 與注釋文本之間插入一個空格,如下面的示例所示。
// The following declaration creates a query. It does not run // the query.
不要在注釋周圍創(chuàng)建格式化的星號塊。
語言準則
以下各節(jié)介紹 C# 遵循以準備代碼示例和樣本的做法。
String 數(shù)據(jù)類型
使用 + 運算符來連接短字符串,如下面的代碼所示。
string displayName = nameList[n].LastName + ", " + nameList[n].FirstName;
若要在循環(huán)中追加字符串,尤其是在使用大量文本時,請使用 StringBuilder 對象。
var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala"; var manyPhrases = new StringBuilder(); for (var i = 0; i < 10000; i++) { manyPhrases.Append(phrase); } //Console.WriteLine("tra" + manyPhrases);
隱式類型的局部變量
當(dāng)變量類型明顯來自賦值的右側(cè)時,或者當(dāng)精度類型不重要時,請對本地變量進行隱式類型化。
// When the type of a variable is clear from the context, use var // in the declaration. var var1 = "This is clearly a string."; var var2 = 27; var var3 = Convert.ToInt32(Console.ReadLine());
當(dāng)類型并非明顯來自賦值的右側(cè)時,請勿使用 var。
// When the type of a variable is not clear from the context, use an // explicit type. int var4 = ExampleClass.ResultSoFar();
請勿依靠變量名稱來指定變量的類型。它可能不正確。
// Naming the following variable inputInt is misleading. // It is a string. var inputInt = Console.ReadLine(); Console.WriteLine(inputInt);
避免使用 var 來代替 dynamic。
使用隱式類型化來確定 for 和 foreach 循環(huán)中循環(huán)變量的類型。
下面的示例在 for 語句中使用隱式類型化。
var syllable = "ha"; var laugh = ""; for (var i = 0; i < 10; i++) { laugh += syllable; Console.WriteLine(laugh); }
下面的示例在 foreach 語句中使用隱式類型化。
foreach (var ch in laugh) { if (ch == 'h') Console.Write("H"); else Console.Write(ch); } Console.WriteLine();
無符號數(shù)據(jù)類型
通常,使用 int 而非無符號類型。 int 的使用在整個 C# 中都很常見,并且當(dāng)你使用 int 時,更易于與其他庫交互。
數(shù)組
當(dāng)在聲明行上初始化數(shù)組時,請使用簡潔的語法。
// Preferred syntax. Note that you cannot use var here instead of string[]. string[] vowels1 = { "a", "e", "i", "o", "u" }; // If you use explicit instantiation, you can use var. var vowels2 = new string[] { "a", "e", "i", "o", "u" }; // If you specify an array size, you must initialize the elements one at a time. var vowels3 = new string[5]; vowels3[0] = "a"; vowels3[1] = "e"; // And so on.
委托
使用簡潔的語法來創(chuàng)建委托類型的實例。
// First, in class Program, define the delegate type and a method that // has a matching signature. // Define the type. public delegate void Del(string message); // Define a method that has a matching signature. public static void DelMethod(string str) { Console.WriteLine("DelMethod argument: {0}", str); } // In the Main method, create an instance of Del. // Preferred: Create an instance of Del by using condensed syntax. Del exampleDel2 = DelMethod; // The following declaration uses the full syntax. Del exampleDel1 = new Del(DelMethod);
異常處理中的 try-catch 和 using 語句
對大多數(shù)異常處理使用 try-catch 語句。
static string GetValueFromArray(string[] array, int index) { try { return array[index]; } catch (System.IndexOutOfRangeException ex) { Console.WriteLine("Index is out of range: {0}", index); throw; } }
通過使用 C# using 語句簡化你的代碼。如果你具有 try-finally 語句(該語句中 finally 塊的唯一代碼是對 Dispose 方法的調(diào)用),請使用 using 語句代替。
// This try-finally statement only calls Dispose in the finally block. Font font1 = new Font("Arial", 10.0f); try { byte charset = font1.GdiCharSet; } finally { if (font1 != null) { ((IDisposable)font1).Dispose(); } } // You can do the same thing with a using statement. using (Font font2 = new Font("Arial", 10.0f)) { byte charset = font2.GdiCharSet; }
&& 和 || 運算符
若要通過跳過必要的比較來避免異常和提高性能,請在執(zhí)行比較時使用 && 來代替 &,使用 || 來代替 | ,如下面的示例所示。
Console.Write("Enter a dividend: "); var dividend = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter a divisor: "); var divisor = Convert.ToInt32(Console.ReadLine()); // If the divisor is 0, the second clause in the following condition // causes a run-time error. The && operator short circuits when the // first expression is false. That is, it does not evaluate the // second expression. The & operator evaluates both, and causes // a run-time error when divisor is 0. if ((divisor != 0) && (dividend / divisor > 0)) { Console.WriteLine("Quotient: {0}", dividend / divisor); } else { Console.WriteLine("Attempted division by 0 ends up here."); }
New 運算符
隱式類型化時,請使用對象實例化的簡潔形式,如下面的聲明所示。
var instance1 = new ExampleClass();
上一行等同于下面的聲明。
ExampleClass instance2 = new ExampleClass();
使用對象初始值設(shè)定項來簡化對象創(chuàng)建。
// Object initializer. var instance3 = new ExampleClass { Name = "Desktop", ID = 37414, Location = "Redmond", Age = 2.3 }; // Default constructor and assignment statements. var instance4 = new ExampleClass(); instance4.Name = "Desktop"; instance4.ID = 37414; instance4.Location = "Redmond"; instance4.Age = 2.3;
事件處理
如果你正定義一個稍后不需要刪除的事件處理程序,請使用 lambda 表達式。
public Form2() { // You can use a lambda expression to define an event handler. this.Click += (s, e) => { MessageBox.Show( ((MouseEventArgs)e).Location.ToString()); }; } // Using a lambda expression shortens the following traditional definition. public Form1() { this.Click += new EventHandler(Form1_Click); } void Form1_Click(object sender, EventArgs e) { MessageBox.Show(((MouseEventArgs)e).Location.ToString()); }
靜態(tài)成員
通過使用類名稱調(diào)用靜態(tài)成員:ClassName.StaticMember。這種做法通過明確靜態(tài)訪問使代碼更易于閱讀。請勿使用派生類的名稱限定基類中定義的靜態(tài)成員。編譯該代碼時,代碼可讀性具有誤導(dǎo)性,如果向派生類添加具有相同名稱的靜態(tài)成員,代碼可能會被破壞。
LINQ 查詢
對查詢變量使用有意義的名稱。下面的示例為位于西雅圖的客戶使用 seattleCustomers。
var seattleCustomers = from cust in customers where cust.City == "Seattle" select cust.Name;
使用別名確保匿名類型的屬性名稱都使用 Pascal 大小寫格式正確大寫。
var localDistributors = from customer in customers join distributor in distributors on customer.City equals distributor.City select new { Customer = customer, Distributor = distributor };
如果結(jié)果中的屬性名稱模棱兩可,請對屬性重命名。例如,如果你的查詢返回客戶名稱和分銷商 ID,而不是在結(jié)果中將它們保留為 Name 和 ID,請對它們進行重命名以明確 Name 是客戶的名稱,ID 是分銷商的 ID。
var localDistributors2 = from cust in customers join dist in distributors on cust.City equals dist.City select new { CustomerName = cust.Name, DistributorID = dist.ID };
在查詢變量和范圍變量的聲明中使用隱式類型化。
var seattleCustomers = from cust in customers where cust.City == "Seattle" select cust.Name;
對齊 from 子句下的查詢子句,如上面的示例所示。
在其他查詢子句之前使用 where 子句,以確保后面的查詢子句作用于經(jīng)過減少和篩選的數(shù)據(jù)集。
var seattleCustomers2 = from cust in customers where cust.City == "Seattle" orderby cust.Name select cust;
使用多行 from 子句代替 join 子句以訪問內(nèi)部集合。例如,Student 對象的集合可能包含測驗分數(shù)的集合。當(dāng)執(zhí)行以下查詢時,它返回高于 90 的分數(shù),并返回得到該分數(shù)的學(xué)生的姓氏。
// Use a compound from to access the inner sequence within each element. var scoreQuery = from student in students from score in student.Scores where score > 90 select new { Last = student.LastName, score };
相關(guān)文章
基于C#的socket編程的TCP異步的實現(xiàn)代碼
本篇文章主要介紹了基于C#的socket編程的TCP異步的實現(xiàn)代碼,詳解的講訴了TCP通信異步的實現(xiàn),有興趣的可以了解一下。2016-11-11WCF基礎(chǔ)介紹并創(chuàng)建簡單應(yīng)用程序
這篇文章介紹了WCF基礎(chǔ)并創(chuàng)建簡單WCF應(yīng)用程序,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01