C# tips

来源:互联网 发布:qq群自定义群地址优化 编辑:程序博客网 时间:2024/05/08 14:28

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"); }

原创粉丝点击