C#6.0新特性

http://files.cnblogs.com/files/aehyok/VS2015CSharp6.0.pdf

一、自动属性初始化

Old:

    public class User
    {
        private bool _isEnabled = true;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { _isEnabled = value; }
        }
    }

New:

    public class User
    {
        public bool IsEnabled { get; set; } = true;//这样就不用另外写一个字段给他赋默认值,直接给属性赋默认值
    }

二、只读属性的初始化:

Old:

    public class User
    {
        private readonly int _id;
        public int Id
        {
            get { return _id; }
        }

        public User(int id)
        {
            _id = id;
        }
    }
New:

public class User
    {
        public int Id { get; }
        public User1(int id)
        {
            Id = id;
        }
    }//只读属性可以和标了<strong>readonly</strong>的字段一样在构造函数里面赋值。
三、用Lambda作为函数体
Old:

    public class User
    {
        public int Id { get; }
        public string Name { get; set; }
        public override string ToString()
        {
            return string.Format("Id:{0} Name:{1}", Id, Name);
        }
    }
New:

    public class User
    {
        public int Id { get; }
        public string Name { get; set; }
        public override string ToString() => string.Format("Id:{0},Name:{1}", Id, Name);//平时总是有一些短小精悍的代码,但我们不得不把他们
        //放到两个括号中,现在我们可以这么写
    }

四、Lambda表达式用作属性 

Old:

    public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }
    }
New:

    public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName=>string.Format("{0} {1}", FirstName, LastName);
    }
五、字符串嵌入值 

    public class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName => $"{FirstName} {LastName}";
        //public string FullName => "\{FirstName} \{LastName}";
    }

六、Using静态类

如果一个静态类里面是一堆方法,比如Math 可以不用写类名,直接调用他的静态方法

七、空值判断

在C#6.0中,我们可以这样写

如果对象为空,则整个表达式的值为空。后面的成员访问不限于方法,还可以是属性,索引器等。

八、nameof表达式

在方法参数检查时,经常会见到这样的代码

里面有那个role是我们手写的字符串,在给role改名时,很容易把下面的那个字符串忘掉,C#6.0解决了这个问题

九、带索引的对象初始化器 

对象初始化器在C#3.0就已经有了,C#6.0的对象初始化器加入了对索引器的支持,使得字典一类的东西也可以轻松初始化

     var numbers = new Dictionary<int, string>
            {
                [7] = "s",
                [9] = "b",
                [13] = "f"
            };
            var obj = new JObject()
            {
                ["firstName"] = "test",
                ["lastName"] = "fff"
            };


十、异常筛选器

       static void Main(string[] args)
        {
            string f = "1";
            try
            {
                AddToRole("");
            }
            catch (ArgumentException ex) when(f!="2")
            {

                Console.WriteLine("ff");
            }

            Console.ReadLine();
        }
在微软的文档中还给出了另一种用法,这个异常会在日志记录失败时抛给上一层调用者

十一、catch和finally 中的 await 


十二、无参数的结构体构造函数

在之前版本的C#中是不允许结构体拥有无参构造函数的,在C#6.0中是允许的,但需要注意一点,通过new得到的结构体会调用结构体中定义的无参构造函数,而通过default得到的不会

调用:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值