記錄 編碼約定 學習程序,
命名空間約定
如果沒有使用using指令,專案也沒有默認匯入合適的命名空間,訪問這些命名空間或者型別時,則需要“完全限定名稱”,
namespace ConsoleApp4 { class Program { static void Main(string[] args) { // 在這里System.Diagnostics是“完全限定名稱” var traceSource = new System.Diagnostics.TraceSource(""); } } }
如果使用了Using指令,則不需要“完全限定名稱”,
using System.Diagnostics; namespace ConsoleApp4 { class Program { static void Main(string[] args) { var traceSource = new TraceSource(""); } } }
代碼布局約定
- 不輕易更改編輯器的設定,通常使用默認的,特別是格式設定和制表符,
- 每行一條陳述句,每行一個宣告,
string[] strs = new string[] { "AA","BB","CC"}; string str = "hello"; int num = 4; if (num == 9) { }
- 一行寫不完的陳述句,斷行寫則需要縮進一個制表符位,除了起頭的第一行,后面無論再斷多少行都是一個制表符位,
string[] strs = new string[] { "AA","BB","CC"}; var query = strs.Where(x => x.Length == 2 && x.Contains("B")). Select(x => new { Name = x, Age = 8 });
- 屬性和方法之間至少一個空行
public int MyProperty { get; set; } public int MyProperty1 { get; set; } void Test() { }
- 使用括號突出運算式的字句,
int val1 = 1; int val2 = 3; int val3 = 4; if ((val1 > val2) && (val1 > val3)) { }
注釋約定
單獨的行,而非代碼末尾;大寫字母開頭,斷行換小寫;句點結束注釋文本;注釋和文本之間留一個空格,
不要在注釋周圍創建格式化的星號塊???沒看懂,
下面放一段ILSpy原始碼
/// <summary> /// Sets the value of a dependency property on <paramref name="targetObject"/> using a markup extension. /// </summary> /// <remarks>This method does not support markup extensions like x:Static that depend on /// having a XAML file as context.</remarks> public static void SetValueToExtension(this DependencyObject targetObject, DependencyProperty property, MarkupExtension markupExtension) { // This method was copied from ICSharpCode.Core.Presentation (with permission to switch license to X11) if (targetObject == null) throw new ArgumentNullException(nameof(targetObject)); if (property == null) throw new ArgumentNullException(nameof(property)); if (markupExtension == null) throw new ArgumentNullException(nameof(markupExtension)); var serviceProvider = new SetValueToExtensionServiceProvider(targetObject, property); targetObject.SetValue(property, markupExtension.ProvideValue(serviceProvider)); }
隱式型別var
當明顯可以從賦值右側推導左側變數型別時,使用var,當精確的型別不太重要時,使用var,
var var1 = "This is clearly a string."; var var2 = 27; var var3 = Convert.ToInt32(Console.ReadLine());
右側型別不明確時,則可以宣告一個明確的型別,
using System; namespace ConsoleApp4 { class Program { static void Main(string[] args) { string test = GetObject(2); int test1 = GetObject(1); decimal test2 = GetObject(3); } static dynamic GetObject(int x) { switch (x) { case 1:return 2; case 2:return ""; default:return 8m; } } } }
在for回圈使用var,下面的i分別推導為int、decimal,
for (var i = 0; i < 10000; i++) { } for(var i = 0m; i < 10m; i = i + 0.1m) { }
在foreach中不建議使用var,
DataTable dataTable = new DataTable(); // 列舉值:EnumerableRowCollection<DataRow>,可以推row為DataRow, foreach (var row in dataTable.AsEnumerable()) { } // 雖然可以推,但宣告明確的型別會讓代碼更加清晰,因為無法在列舉泛型型別里明顯看出當前row的型別, foreach (DataRow row in dataTable.AsEnumerable()) { } // 列舉值:DataRowCollection,不宣告明確型別row則為object?,因此需要宣告明確的型別, foreach (DataRow row in dataTable.Rows) { }
符號型別
使用時,符號型別優先于無符號型別,short、int、long, ushort、uint、ulong,
陣列
使用間接的語法宣告陣列
// 這時候,無法從右側推導變數型別, string[] vowels1 = { "a", "e", "i", "o", "u" }; // 可以推導,左側使用var var vowels2 = new string[] { "a", "e", "i", "o", "u" }; var vowels3 = new string[2]; vowels3[0] = "a"; vowels3[1] = "e";
委托
使用簡短的語法為委托賦值,
static void Main(string[] args) { Del exampleDel2 = DelMethod; var del3 = new Del(DelMethod); } public delegate void Del(string message); public static void DelMethod(string str) { Console.WriteLine("DelMethod argument: {0}", str); }
Using
如果使用try...finally...,且在finally只有釋放資源的代碼,考慮替換成using,using會自動呼叫Dispose方法,
Font font1 = new Font("Arial", 10.0f); try { } finally { font1.Dispose(); } using(Font font=new Font("Arial", 10.0f)) { }
New運算子
能推導型別使用var,物件實體的建構式沒有引數則不必呼叫建構式,
// 不必使用new ExampleClass() var instance3 = new ExampleClass { Name = "Desktop", ID = 37414, Location = "Redmond", Age = 2.3 }; var instance4 = new ExampleClass(); instance4.Name = "Desktop"; instance4.ID = 37414; instance4.Location = "Redmond"; instance4.Age = 2.3;
事件處理
在事件比較簡短、且只使用一次的情況下,可以考慮使用lambda運算式,
public Form1() { InitializeComponent(); this.Click += (s, e) => { MessageBox.Show(s.ToString()); }; }
靜態成員
派生物件時,不建議添加和基類具有相同名稱的靜態成員,
using System; namespace ConsoleApp4 { class Program { static void Main(string[] args) { ExampleClass2.Test(); } } class ExampleClass { public static void Test() { Console.WriteLine("呼叫ExampleClass Test"); } } class ExampleClass2:ExampleClass { // 同名容易引起混亂,隱性覆寫了ExampleClass的Test方法, public static void Test() { Console.WriteLine("呼叫ExampleClass2 Test"); } } class ExampleClass3 : ExampleClass { // 使用new,說明有意覆寫, public new static void Test() { } } }
Linq查詢運算式
匿名型別遵循Pascal大小寫格式(與駱駝命名法類似,駱駝命名法是首字母小寫,而Pascal帕斯卡命名法是首字母大寫),
var localDistributors = from customer in customers join distributor in distributors on customer.City equals distributor.City select new { Customer = customer, Distributor = distributor, CustometorId = customer.ID };
在查詢變數和范圍變數的宣告中使用隱式型別化,雖然查詢變數沒有明確標有var關鍵字,但確實隱式化了,
using System; using System.Linq; namespace ConsoleApp4 { class Program { static void Main(string[] args) { var customers = new System.Collections.Generic.List<Customer>(); var distributors = new System.Collections.Generic.List<Distributor>(); var localDistributors = from customer in customers join distributor in distributors on customer.City equals distributor.City select new { Customer = customer, Distributor = distributor, CustometorId = customer.ID }; } } internal class Distributor { public object City { get; internal set; } } internal class Customer { public object City { get; internal set; } public object ID { get; internal set; } } }
對其from字句下面的查詢陳述句,
// 第一種:from陳述句不和宣告變數同一行,則在下一行縮進一個制表符,
var localDistributors = from customer in customers join distributor in distributors on customer.City equals distributor.City select new { Customer = customer, Distributor = distributor, CustometorId = customer.ID };
// 第二種:from陳述句和宣告變數同一行 var localDistributors = from customer in customers join distributor in distributors on customer.City equals distributor.City select new { Customer = customer, Distributor = distributor, CustometorId = customer.ID };
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/69584.html
標籤:其他
