从今天开始我希望能够踏踏实实的把c#的每个知识点学习到并记录下来,以此来增长自己的基础知识。
任何语言在学习之前都应该了解和遵循它的编码规范来做,这样的好处有这几点:
-
它们为代码创建一致的外观,以确保读取器专注于内容而非布局。
-
它们使得读取器可以通过基于之前的经验进行的假设更快地理解代码。
-
它们便于复制、更改和维护代码。
-
它们展示 C# 最佳做法
1:string 数据类型的使用
若要在循环中追加字符串,尤其是在使用大量文本时,请使用 StringBuilder 对象。
var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala" ;var manyPhrases = new StringBuilder();for ( var i = 0; i < 10000; i++){manyPhrases.Append(phrase);}
2:隐式类型的局部变量
当变量类型明显来自赋值的右侧时,或者当精度类型不重要时,请对本地变量进行隐式类型化。
// 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());
当类型并非明显来自赋值的右侧时,请勿使用 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 循环中循环变量的类型。
下面的示例在 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();
比如:// 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.
// 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 语句。
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 方法的调用),请使用 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; }
// 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;
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()); }
ps:去学习一个东西就要真正了解,运用,熟练使用它,不能一知半解,含糊不清的过目了事。这是程序员的大忌 = =