C# 6.0新特性

C#6.0新特性

一、C#发展历程

一、C#6.0新特性

转自:https://www.cnblogs.com/yinrq/p/5600530.html

1、字符串插值 (String Interpolation)

字符串拼接优化

Before:

var Name = "joye.net";
var Results = "Hello" + Name;//直接拼接
var results1 = string.Format("Hello {0}", Name);//Format拼接

After:

var results2 = $"Hello {Name}"; //$拼接
var results= $"Hello {Name}{new Program().GetCnblogsSite()}";//{}可以直接插入代码

2、null检查运算符【 ?.】 (Monadic null checking)

null优化

Before:

        public static string GetCnblogsSite()
        {
            return "http://www.cnblogs.com/yinrq";
        } 
Program pro = null;
if(pro!=null)
      Console.WriteLine(GetCnblogsSite());

After:

Program pro = null;
Console.WriteLine(pro?.GetCnblogsSite());

3、   自动属性初始化器(Initializers for auto-properties)

可以直接给自动属性赋值了,不需要写在构造函数中。

Before:

    public class ClassA
    {
        private string Name{get;set;};
        public ClassA()
        {
            Name = "joye.net";
        } 
    }

After:

    public class ClassA
    {
        public string Name { get; set; } ="joye.net";
   
    }

 4、只读自动属性(Getter-only auto-properties)

只读自动属性可以直接初始化,或者在构造函数中初始化。

before

 //缩小自动属性的访问权限
    public class ClassA
    {
        public string Name { get; private set; }
   
    }
    //C#1.0实现
    public class ClassA
    {
        private string Name = "joye.net";

        public string Name
        {
            get { return Name; }
        }
    }

after:

    public class ClassA
    {
        public string Name { get; } = "joye.net";
    }

5、表达式方法体(Property Expressions && Method Expressions)

只读属性,只读索引器和方法都可以使用Lambda表达式作为Body。

一句话的方法体可以直接写成箭头函数,而不再需要大括号(分页控件http://www.cnblogs.com/yinrq/p/5586841.html就用到了属性表达式Property Expressions)

    public class PagerInBase
    {
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 页数
        /// </summary>
        public int PageSize { get; set; }
     
//以前的写法
     //public int Skip{get{return (PageIndex - 1) * PageSize}}
//跳过序列中指定数量的元素 public int Skip => (PageIndex - 1) * PageSize; /// <summary> /// 请求URL /// </summary> public string RequetUrl => System.Web.HttpContext.Current.Request.Url.OriginalString; /// <summary> /// 构造函数给当前页和页数初始化 /// </summary> public PagerInBase() { if (PageIndex == 0) PageIndex = 1; if (PageSize == 0) PageSize = 10; } }

方法表达式(Method Expressions)

        //before 的完整方法
        public int Skip()
        {
             return (PageIndex - 1) * PageSize
        }
        //After C#6.0 方法表达式
        public int Skip() => (PageIndex - 1) * PageSize;

6、using静态类(Static type using statements)

using System;
using static System.Math;
namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(Log10(5) + PI);
            }
        }
    }

 7、检查方法参数nameof表达式(nameof expressions)

这个很有用,原来写WPF中的ViewModel层的属性变化通知时,需要写字符串,或者使用MvvmLight等库中的帮助方法,可以直接传入属性,但由于是在运行时解析,会有少许性能损失。现在使用nameof运算符,保证重构安全和可读性,又提升了性能。

Before:

        public static void Add(Person person)
        {
            if (person == null)
            {
                throw new ArgumentNullException("person");
            }
        }

After:

        public static void Add(Person person)
        {
            if (person == null)
            {
                throw new ArgumentNullException(nameof(person));
            }
        }

8、带索引的对象初始化器(Index initializers )

直接通过索引进行对象的初始化

var dic = new Dictionary<int, string> { [0]="joye.net",[1]= "http://yinrq.cnblogs.com/",[2]= "Index initializers " };

9、catch和finally 中使用await (catch和finally 中的 await )

在C#5.0中,await关键字是不能出现在catch和finnaly块中的。而C#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(); // finally and do this. 
            }

10、内联out参数(Inline declarations for out params)

before

int x;
int.TryParse("123", out x);

after:

int.TryParse("123", out int x);

11、无参数的结构体构造函数(Parameterless constructors in structs)

    public struct MyStruct
    {
        public int A { get; }
        public int B { get; }
        public MyStruct(int a, int b) { A = a; B = b; }
        public MyStruct(): this(0, 1) { }
    }
     WriteLine(new MyStruct().ToString());
     WriteLine(default(MyStruct).ToString());


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40883328

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值