zoukankan      html  css  js  c++  java
  • VS2015中C#版本6.0的新特性

    [z]http://www.cnblogs.com/xszjk/articles/6417173.html

    [z]https://www.cnblogs.com/qixu/p/6047229.html

    注意:这些新特性只能用于VS2015及更高版本,无法在VS2013、VS2010等低版本中使用。当然,如果你不喜欢这些新的特性,仍然可以继续使用原来的用法(所以说它是新的语法糖)。
     1、自动属性初始化的改进(有用)
     原来的用法(声明时无法同时初始化),例如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    class MyClass
    {
      public int Age { get; set; }
      public string Name { get; set; }
      public MyClass()
      {
        Age = 20;
        Name = "张三";
      }
    }

    新用法(声明时可同时初始化,更方便了),例如:

    1
    2
    3
    4
    5
    class MyClass
    {
      public int Age { get; set; } = 20;
      public string Name { get; set; } = "张三";
    }

    2、String.Format的改进(有用)
     原来的用法:用string.Format(…)实现,例如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class MyClass
    {
      public void MyMethod()
      {
        string name = "张三";
        int age = 20;
        string s1 = string.Format("{0},{1}", name, age);
        string s2 = string.Format("姓名={0},年龄={1}", name, age);
        string s3 = string.Format("{0,15},{1:d3}", name, age);
        string s4 = string.Format("{0,15},{1,10:d3}", name, age);
        Console.WriteLine("{0},{1},{2},{3}", s1, s2, s3 ,s4);
        string s5 = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
      }
    }

    新用法:用“$”前缀实现(变量直接写到大括号内,而且带智能提示,更方便了),例如: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class MyClass
    {
      public void MyMethod()
      {
        string name = "张三";
        int age = 20;
        string s1 = $"{name},{age}";
        string s2 = $"姓名={name},年龄={age}";
        string s3 = $"{name,15},{age:d3}";
        string s4 = $"{name,15},{age,10:d3}";
        Console.WriteLine($"{s1},{s2},{s3},{s4}");
        string s5 = $"{DateTime.Now:yyyy-MM-dd}";
      }
    }

    3、字典的初始化
     原来的用法: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    class MyClass
    {
      public void MyMethod()
      {
        Dictionary<string, int> student = new Dictionary<string, int>();
        student.Add("a1", 15);
        student.Add("a2", 14);
        student.Add("a3", 16);
      }
    }

    新用法(可以直接写初始化的值,更方便了): 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class MyClass
    {
      public void MyMethod()
      {
        Dictionary<string, int> student = new Dictionary<string, int>()
        {
          ["a1"] = 15,
          ["a2"] = 14,
          ["a3"] = 16
        };
      }
    }

    4、可以用static声明静态类的引用
     原来的用法: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    using System;
    namespace MyApp
    {
      class Demo1New
      {
        public static double MyMethod(double x, double angle)
        {
          return Math.Sin(x) + Math.Cos(angle);
        }
      }
    }

    新用法(表达式比较复杂的时候有用,代码更简洁了):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    using static System.Math;
    namespace MyApp
    {
      class Demo1New
      {
        public static double MyMethod(double x, double angle)
        {
          return Sin(x) + Cos(angle);
        }
      }
    }

    5、nameof表达式
     假定WPF应用程序中有下面的类: 

    1
    2
    3
    4
    5
    6
    public class MyClass
     {
      
    public string MyText { get; set; } = "aaa";
      
    }

     并假定有下面的XAML代码:
     <StackPanel>
     
    <TextBlock Name="txt1"/>
     
    ……
     
    </StackPanel>
     代码隐藏类中原来的用法:
     txt1.SetBinding(TextBlock.TextProperty, "MyText"); 
    现在的用法(因为有错误检查智能提示,用起来更方便了):
     txt1.SetBinding(TextBlock.TextProperty, nameof(MyClass.MyText)); 
    6、Null-条件表达式
    (有用)

    1
    2
    3
    4
    var ss = new string[] { "Foo", null };
    var length0 = ss [0]?.Length; // 结果为3
    var length1 = ss [1]?.Length; // 结果为null
    var lengths = ss.Select (s => s?.Length ?? 0); //结果为[3, 0]

    7、在try-catch-finally中使用await
     异步编程中,原来在catch或者finally中无法使用await,现在可以了: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    async void SomeMethod()
    {
      try
      {
        //...etc...
      }
      catch (Exception x)
      {
        var diagnosticData = await GenerateDiagnosticsAsync (x);
        Logger.log (diagnosticData);
      }
      finally
      {
        await someObject.FinalizeAsync();
      }
    }
     
     

    在C#中??和?分别是什么意思?

     
    1. 可空类型修饰符(?):
    引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空。
    例如:string str=null; 是正确的,int i=null; 编译器就会报错。
    为了使值类型也可为空,就可以使用可空类型,即用可空类型修饰符"?"来表示,表现形式为"T?"
    例如:int? 表示可空的整形,DateTime? 表示可为空的时间。
    T? 其实是System.Nullable(泛型结构)的缩写形式,也就意味着当你用到T?时编译器编译 时会把T?编译成System.Nullable的形式。
    例如:int?,编译后便是System.Nullable的形式。

    2. 三元(运算符)表达式(?:):
    例如:x?y:z 表示如果表达式x为true,则返回y;如果x为false,则返回z,是省略if{}else{}的简单形式。

    3. 空合并运算符(??):
    用于定义可空类型和引用类型的默认值。如果此运算符的左操作数不为null,则此运算符将返回左操作数,否则返回右操作数。
    例如:a??b 当a为null时则返回b,a不为null时则返回a本身。
    空合并运算符为右结合运算符,即操作时从右向左进行组合的。如,“a??b??c”的形式按“a??(b??c)”计算。
  • 相关阅读:
    swift中的"类型擦除"
    Swift Pointer 使用指南
    Swift Method Dispatching — a summary of my talk at Swift Warsaw
    Swift库二进制接口(ABI)兼容性研究
    编译器最重要的工作就是确定对象内存模型
    Objective-C类成员变量深度剖析--oc对象内存模型
    Swift进阶之内存模型和方法调度
    Swift 对象内存模型探究(一)
    编程领域中的 "transparent" 和 "opaque"
    structure vs class in swift language
  • 原文地址:https://www.cnblogs.com/jjj250/p/10179350.html
Copyright © 2011-2022 走看看