C#6新特性

1.自动属性增强

public bool ITuest { get; set; } = true;

 public string FirstName { get; } = "aehyok";

2.拉姆表达式作为函数体

 public int Move(int x, int y) => x + y;

3.引用静态类using static

 public double ReturnNum() =>  Acos(3);

4.空值判断null

int? length = customers?.Count;
 int first = (int)customers?[0];



5.字符串嵌入值

  public string MakeStr()
        {
            string firstName = "ligen";
            string lastName = "wanli";
            return string.Format("one is /{firstName},two is /{lastName}");
        }


6.nameof表达式nameof expressions

public static void AddCustomer(Customer customer)
{
     if (customer == null )
     {
         throw new ArgumentNullException( nameof(customer) );
     }
}
7.带索引的对象初始化器Index initializers
var numer = new Dictionary<int, string> {[4] = "s33" };
8
try
{
     res = await Resource.OpenAsync(…); // You could do this. …
}
catch (ResourceException e)
{
     await Resource.LogAsync(res, e); // Now you can do this …
} finally
{
     if (res != null )
         await res.CloseAsync(); // … and this.
}

9、catch和finally 中的 await —— Await in catch and finally blocks

 在C#5.0中,await关键字是不能出现在catch和finnaly块中的。而在6.0中

            try
            {
                res = await Resource.OpenAsync(…); // You could do this. … 
            }
            catch (ResourceException e)
            {
                await Resource.LogAsync(res, e); // Now you can do this … 
            } finally
            {
                if (res != null)
                    await res.CloseAsync(); // … and this. 
            } 

10Parameterless struct ctors 无参数的结构体构造函数

public FractionStruct( int a, int b) { A = a; B = b; }

public FractionStruct() : this(0, 1) { }

结构体可以提供自定义的无参数构造函数了。

new FractionStruct()

default(FractionStruct)

new是调用无参数构造函数。

default是不调用无参数构造函数。

11.Null propagation 空传播

v?.A

v?["B"]

v?.ToString()

对象为null时不调用属性,索引器,方法等,表达式返回null,和Swift中的用法相似
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值