首页 > Python基础教程 >
-
C#教程之Static Using
Static Using
static using声明允许直接调用静态方法而不需要指定类名:
C# 5
static void StaticUsingDemoInCSharp5(string output) { Console.WriteLine(output); // Call the static method WriteLine with class name Console }
C# 6
using static System.Console; namespace NewFeatureTest { class Program { static void Main(string[] args) { StaticUsingDemoInCSharp6("Hello, C# 6"); } static void StaticUsingDemoInCSharp6(string output) { WriteLine(output); } } }
Expression-Bodied Methods
使用expression-bodied methods,一个只有一句statement的函数可以使用lambda写法。
C# 5
private static bool IsIntegerEqual(int a, int b) { return a == b; }
C# 6
private static bool IsIntegerEqualWithExpressionBodied(int a, int b) => a == b;
Expression-Bodied Properties
和expression-bodied methods类似,只支持get访问器且只有一句statement的属性(Properties)也可以使用lambda写法。
C# 5
private string firstName; public string FirstName { get { return firstName; } }
C# 6
private string lastName; public string LastName => lastName;
Auto-Implemented Property Initializers
Auto-implemented Property可以在一个属性初始化器里初始化。
C# 5
public string Sex { get; set; } public Person() { Sex = "Male"; }
C# 6
public int Age { get; set; } = 42; // The age will be initialized to 42 when object constructed
Read-Only Auto Properties
在之前的C#版本中,只读属性要求完整的属性语法。C#6提供了自动实现的版本:
C# 5
private readonly string homeTown; public string HomeTown { get { return homeTown; } }
C# 6
public string BirthDay { get; } public Person(string _firstName, string _lastName) { BirthDay = DateTime.Today.ToString(); }
nameof Operator
使用最新的nameof操作符,字段(fields)、属性(Properties)、方法和类型的名字都能被取到。有了它,妈妈在也不用担心改名字的时候会漏掉啦。
C# 5
public bool IsAdult(int age) { if (age < 0) { throw new ArgumentException("age"); } return age >= 18; }
C# 6
public bool IsAdultWithNameOfOperator(int age) { if (age < 0) { throw new ArgumentException(nameof(age)); } return age >= 18; }
Null Propagation Operator
null propagation操作符能够大大滴简化空对象的检查。
C# 5
public Person(int? _age) { Age = _age == null ? -1 : _age.Value; }
C# 6
public Person(DateTime? _birthDay) { birthDay = _birthDay?.ToString(); }
String Interpolation
在c#6中,字符串插值不需要再call string.Format方法啦。相较于之前在string中使用数字占位符如{0},C#6支持表达式占位符如{age}。
C# 5
public override string ToString() { return string.Format("{0} {1}”, firstName, lastName); }
C# 6
public override string ToString() => $"{firstName} {lastName}";
在这里,我们使用了新的string interpolation和expression-bodied method让整个代码相较于之前简洁很多(按代码行数算钱的码农悲催了)。
Dictionary Initializers
在C# 6中,我们可以像集合初始化器一样使用字典(Dictionary)初始化器来初始化字典对象。
C# 5
var dictInCSharp5 = new Dictionary<int, string>() { {2, "Two"}, {1, "One"} };
C# 6
var dictInCSharp6 = new Dictionary<int, string>() { [2] = "Two", [1] = "One" };
这东西个人觉得没什么用,反而容易和Collection的下标混淆。
Exceptions Filters
异常过滤器允许我们在catch异常之前使用条件来过滤异常。
C# 5
try { StaticUsingDemoInCSharp6(p.LastName); } catch (Exception e) { if (e.Message.Contains("Aha")) { throw; } }
C# 6
try { StaticUsingDemoInCSharp6(p.LastName); } catch(Exception e) when (e.Message.Contains("Aha")) { throw; }
Await in Catch
C#6允许我们在catch程序块里面使用await关键字。在c#5我们只能workaround。
C# 5
private static async Task AwaitInCatchDemoInCSharp5() { bool hasError = false; string errorMessage = string.Empty; try { throw new FieldAccessException("No Permission."); } catch(FieldAccessException e) { hasError = true; errorMessage = e.Message; } if(hasError) { await MessageDialog().ShowAsync(errorMessage); } }
C# 6
private static async Task AwaitInCatchDemoInCSharp5() { try { throw new FieldAccessException("No Permission."); } catch(FieldAccessException e) { await MessageDialog().ShowAsync(e.Message); } }