C# tips

1. .NET 3.0 introduces a new concept of Auto-implemented properties. In short you can do things like: public class MyClass { public int X { get; set; } } Instead of: public class MyClass { private int _x; public int X { get { return _x; } set { _x = value; } } } 2. C# offers the ability to make things like ‘int’ a nullable type. int? x = null; if (x == null || !x.HasValue) { x = 5; } Console.WriteLine(x.ToString()); 3. Aliases make it easier for you to shortcut to your most common called classes. using c = System.Console; using myMethod = System.Reflection.MethodBase; class Program { static void Main(string[] args) { c.WriteLine(myMethod.GetCurrentMethod().Name); } } 4. A favorite shortcut of mine is ‘Ctrl+R+M’ lets you turn any segment of highlighted code into a new method. 5. .NET 4 has a new feature for keeping track of parameters that you may be passing in, you can now prepend values / variables with labels: static void Main(string[] args) { File.Copy(sourceFileName: "Myfile.txt", destFileName: "dest.txt"); }

發佈了40 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章