• c# 6.0新特性(一)


    写在前面

    接近年底了,基本上没什么活了,就学点新东西,就想着了解下c# 6.0的新特性。在code project上看到了一篇不错的文章,就准备翻译一下,顺便照着学习学习。废话不多说,直奔主题。

    原文地址:http://www.codeproject.com/Articles/1070659/All-About-Csharp-New-Features

    简介

    众所周知,c# 6.0 是在visual studio 2015中引入的。在其他的几个版本中同样引入一些特性,比如在c# 3.0中引入了linq,在c# 4.0中引入了动态类型dynamic,在c#5.0中引入async和await等等。

    在c# 6.0更多关注了语法的改进,而不是增加新的功能。这些新的语法将有助于我们更好更方便的编写代码。

    背景

    你可以通过下载visual studio2015或者你已经安装了visual studio2013,然后从这里(https://github.com/dotnet/roslyn)下载roslync包。Roslync是一个开源的并且和visual studio 2015继承的.net 编译器。

    以下是c# 6.0的几种改善的新的语法:

    1、Using Static:使用static

    2、Auto Property Initializers:自动属性初始化

    3、Index Initializers:索引初始化

    4、String Interpolation:字符串插入

    5、Expression Bodied Members

    6、Getter Only Auto Properties:只读自动属性

    7、Exception Filters:异常过滤器

    8、Null Conditional Operators:null条件操作符

    9、Declaration Expressions:声明表达式

    using static

    这是一件在你的编程生涯中非常有用的一个特性。现在你可以通过using 关键字访问类的属性和方法了。实现这个,你只需要在你类的命名空间前using之后static关键字就可以了。

    using static System.Console;

    当我们使用using引用了System.Console这个类。那么我们就可以访问这个类的所有属性和方法。下面看一个在c# 6.0之前和之后我们是如何做的例子:

    Before C# 6.0

    using System;
    namespace CplusplusNewFeature
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(" C# 6.0新特性");
            }
        }
    }

    在c# 6.0之前,如果我们要使用WrilteLine()方法,我们使用Console.WriteLine()。如果没有引用Console这个类,我们是无法访问这个方法的。

    C# 6.0中

    using System;
    using static System.Console;
    using static CplusplusNewFeature.MyClass;
    namespace CplusplusNewFeature
    {
        class Program
        {
            static void Main(string[] args)
            {
                WriteLine("c# 6.0 新特性");
                Hello();
                ReadLine();
            }
        }
        static class MyClass
        {
            public static void Hello()
            {
                WriteLine("This is static class");
            }
        }
    }

    C# 6.0在使用这个类的时候并不需要每次都引用这个类。我们只需要在using中声明这个类,那么我们就可以访问到它。

    当然,我们也可以引用其他的类并访问它的成员。

    在上面的例子中,我们使用了MyClass类的Hello()方法。

     static class MyClass
        {
            public static void Hello()
            {
                WriteLine("This is static class");
            }
        }

    在这里我们使用using引入了命名空间了,如下所示:

    using static CplusplusNewFeature.MyClass;

    Auto Property Initializers

    我们使用属性访问内部成员。属性有setter和getter方法。在c# 6.0之前,我们并不能直接为属性赋值。如果真要这么做,我们只能通过属性对应的字段来初始化。但是c# 6.0提供更灵活的方式。

    通过c# 6.0我们可以在定义属性的时候直接为它赋值。

    Before C# 6.0

    之前我们通过构造函数初始化属性。看下面的例子,在这里我们创建了多个属性,并在构造函数中为他们赋值。

    using System;
    using static System.Console;
    namespace CplusplusNewFeature
    {
        public class Program
        {
            static void Main(string[] args)
            {
                Employee emp = new Employee();
                Console.WriteLine("Employee Id is " + emp.EmployeeId);
                Console.WriteLine("Employee Full Name is " + emp.FullName);
                Console.ReadLine();
            }
        }
        public class Employee
        {
            public Guid EmployeeId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
    
            public string FullName { get; set; }
            public Employee()
            {
                EmployeeId = Guid.NewGuid();
                FirstName = "Mukesh";
                LastName = "Kumar";
                FullName = string.Format("{0} {1}", FirstName, LastName);
            }
        }
    }

    C# 6.0中

    但是在c#6.0中非常方便,我们不必担心怎么、在哪儿初始化属性值。你可以直接在属性后面通过=asign指定属性的值。这种方式不会引发异常并且可以正常运行。在下面的例子中,我们可以看到EmplyeeId,FirstName,lastName初始化。

    using System;
    using static System.Console;
    namespace CplusplusNewFeature
    {
        public class Program
        {
            static void Main(string[] args)
            {
                Employee emp = new Employee();
                WriteLine("Employee Id is " + emp.EmployeeId);
                WriteLine("Employee Full Name is " + emp.FullName);
                ReadLine();
            }
        }
        public class Employee
        {
            public Guid EmployeeId { get; set; } = Guid.NewGuid();
            public string FirstName { get; set; } = "Mukesh";
            public string LastName { get; set; } = "Kumar";
    
            public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
    
        }
    }

    Index Initializers

    c#6.0提供了一种新的初始化集合的方式。你可以创建想字典,hashtable一样的集合。众所周知,字典是键值对形式的,并且为对应的key指定value。在c#6.0之前,我们有很多不同的方式去创建键值对。看一下下面的在c#中c#6.0之前怎么使用键值对字典的。

    using System;
    using System.Collections.Generic;
    namespace CplusplusNewFeature
    {
        public class Program
        {
            static void Main(string[] args)
            {
                Dictionary<int, string> myDictionary = new Dictionary<int, string>() {
                    {1, "Mukesh Kumar" },
                    {2, "Rahul Rathor" },
                    {3, "Yaduveer Saini" },
                    {4, "Banke Chamber" }
                };
    
                foreach (var item in myDictionary)
                {
                    Console.WriteLine("The " + item.Key + " Number Employee is " + item.Value + "
    ");
                }
                Console.ReadLine();
            }
        }
    }

    In c#6.0

    但是在c#6.0中,我们可以逻辑上为索引为1的指定“Mukes Kumar”的值,其它的以此类推。你可以看看下面的这个例子,将消除你所有的疑虑。

    using System;
    using System.Collections.Generic;
    using static System.Console;
    namespace CplusplusNewFeature
    {
        public class Program
        {
            static void Main(string[] args)
            {
                Dictionary<int, string> myDictionary = new Dictionary<int, string>()
                {
                    [1] = "Mukesh Kumar",
                    [2] = "Rahul Rathor",
                    [3] = "Yaduveer Saini",
                    [4] = "Banke Chamber"
                };
    
                foreach (var item in myDictionary)
                {
                    WriteLine("The " + item.Key + " Number Employee is " + item.Value + "
    ");
                }
                ReadLine();
            }
        }
    }

    总结

    之前也看过这方面的文章,没动手实现过,今天尝试了下,发现非常的方便。

     

  • 相关阅读:
    csu 1513 Kick the ball! 搜索
    训练赛bug总结
    csu 1780 简单的图论问题? 搜索
    贪吃蛇
    hdu 1541 Stars 树状数组
    FZU 2092 收集水晶 BFS记忆化搜索
    [ An Ac a Day ^_^ ] UVALive 2035 The Monocycle BFS
    52. N皇后 II
    修改全局变量-global 修改外部嵌套函数中的变量 nonlocal
    安装glove 不报错
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/5168217.html
Copyright © 2020-2023  润新知