Best 5 C# 6.0 Language Features

本文介绍了C#6.0中的五大新特性:表达式主体成员让单一逻辑的实现更为简洁;条件访问运算符简化了空值检查;自动属性初始化使属性赋值更加便捷;在捕获块中使用异步等待提升异常处理能力;异常过滤增强异常处理的灵活性。
摘要由CSDN通过智能技术生成

Best 5 C# 6.0 Language Features

# Expression Bodied Methods
How many times have you had to write a method just for one line of code? Now, with C# 6 you can simply create an expression bodied member with only the expression and without the curly braces or explicit returns.

class Employee
{
   // Method with only the expression
   public static int
      CalculateMonthlyPay(int dailyWage)
      => dailyWage * 30;
}
# ?—Conditional Access Operator
In earlier versions of the C# language, you always had to write the explicit if condition NULL checks before using an object or its property, as shown below.

private void GetMiddleName(Employee employee)
{
   string employeeMiddleName = "N/A";

   if (employee != null && employee.EmployeeProfile
      != null)
      employeeMiddleName =
         employee.EmployeeProfile.MiddleName;
}
# Auto-Property Initializers
With the Auto-Property initialization feature, the developer can initialize properties without using a private set or the need for a local variable. Following is the sample source code.

class PeopleManager
{
   public List<string> Roles { get; } =
      new List<string>() { "Employee", "Managerial"};
}
# Await in the Catch Block
This is an important non-syntactic enhancement that will be available in C# 6. The await keyword can be called inside the catch and finally blocks. This opens up the way to perform an async exception handling or fallback process in case an exception happened during an async process call.

public async void Process()
{
   try
   {
      Processor processor = new Processor();
      await processor.ProccessAsync();
   }
   catch (Exception exception)
   {
      ExceptionLogger logger = new ExceptionLogger();
      // Catch operation also can be aync now!!
      await logger.HandleExceptionAsync(exception);
   }
}
# Exception Filters
Exceptions can be filtered in the catch blocks with ease and cleanly with C# 6. Following is a sample source code where the intention is to handle all Exceptions except the SqlException type.

public async void Process()
{
   try
   {
      DataProcessor processor = ne
   }
   // Catches and handles only non sql exceptions
   catch (Exception exception) if(exception.GetType()
      != typeof(SqlException))
   {
      ExceptionLogger logger = new ExceptionLogger();
      logger.HandleException(exception);
   }
}

I hope this article gives a highlight of the features that can be expected with the actual RTM of C# 6. Altshough it doesn't seem to have any breaking features, it definitely has the improvements to make your code look cleaner, better, and easier. Integration of the Roslyn compiler is definitely in the lime light
 

转载于:https://www.cnblogs.com/glenblogs/p/4427289.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值