C#6.0特性(快来围观)

说明一下,很多博友一进来就认为仅仅是语法糖,C#语法的更新,代表着它的进步,语法糖是为了让我们更好的实现语句和功能,增加了易读性和易用性。而且它的每次进步,也会给我们带来新的支持和改进。比如C#(4.5/5)中出现的await,async这样异步编程的语法,可不仅是语法糖哦,这次又在Catch和finally中增加支持,大大增加异步编程的编程效率和实现方式的简化程度。

1、自动属性的增强

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

C#4.0下的果断实现不了的。

C#6.0中自动属性的初始化方式

只要接触过C#的肯定都会喜欢这种方式。真是简洁方便呀。

 

 1.2、只读属性初始化Getter-only auto-properties

先来看一下我们之前使用的方式吧

    public class Customer
    {
        public string Name { get; }

        public Customer(string firstName,string lastName)
        {
            Name = firstName +" "+ lastName;
        }
    }

再来看一下C#6.0中

    public class Customer
    {
        public string FirstName { get; }="aehyok";
        public string LastName { get; }="Kris";

    }

和第一条自动属性初始化使用方式一致。

2、Expression bodied function members

2.1 用Lambda作为函数体Expression bodies on method-like members

public Point Move(int dx, int dy) => new Point(x + dx, y + dy);  

再来举一个简单的例子:一个没有返回值的函数

public void Print() => Console.WriteLine(FirstName + " " + LastName);

 

2.2、Lambda表达式用作属性Expression bodies on property-like function members

        public override string ToString()
        {
            return FirstName + " " + LastName;
        }

现在C#6中

    public class User
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public override string ToString() => string.Format("{0}——{1}", FirstName, LastName);

        public string FullName => FirstName + " " + LastName;
    }

 

3、引用静态类Using Static 

 在Using中可以指定一个静态类,然后可以在随后的代码中直接使用静态的成员

 

4、空值判断Null-conditional operators  

 直接来看代码和运行结果

 通过结果可以发现返回的都为null,再也不像以前那样繁琐的判断null勒。

 

5、字符串嵌入值    

在字符串中嵌入值

之前一直使用的方式是

现在我们可以简单的通过如下的方式进行拼接

6、nameof表达式nameof expressions 

 在方法参数检查时,你可能经常看到这样的代码(之前用的少,这次也算学到了)

        public static void AddCustomer(Customer customer)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }
        }

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

        public static void AddCustomer(Customer customer)
        {
            if (customer == null)
            {
                throw new ArgumentNullException(nameof(customer));
            }
        }

 

7、带索引的对象初始化器Index initializers   

 直接通过索引进行对象的初始化,原来真的可以实现

通过这种方式可以发现字典中只有三个元素,所以也就只有这三个索引可以访问额,其他类型的对象和集合也是可以通过这种方式进行初始化的,在此就不进行一一列举了。

8、异常过滤器 (Exception filters)  

先来看一个移植过来的方法

            try
            {
                var numbers = new Dictionary<int, string> {[7] = "seven",[9] = "nine",[13] = "thirteen" };
            }
            catch (ArgumentNullException e)
            {
                if (e.ParamName == "customer")
                {
                    Console.WriteLine("customer can not be null");
                }
            }

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

        private static bool Log(Exception e)
        {
            ///处理一些日志
            return false;
        } 

        static void Main(string[] args)
        {

            try
            {
                ///
            }
            catch (Exception e){if (!Log(e))
                {

                }
            }

            Console.ReadLine();
        }

 

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. 
            } 

 

10、无参数的结构体构造函数—— Parameterless constructors in structs 

 

(摘自:http://www.cxyclub.cn/n/63739/

转载于:https://www.cnblogs.com/HJL-Blog/p/4457632.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including:, A Refined ADO.NET Entity Framework Programming ModelNumerous IDE and MVVM Enhancements for WPF Desktop DevelopmentNumerous updates to the ASP.NET Web APIs, This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies)., The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms., What You Will Learn:, Be the first to understand the .NET 4.6 platform and C# 6.Discover the ins and outs of the leading .NET technology.Learn from an award-winning author who has been teaching the .NET world since version 1.0.Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值