C#2.0
  • 泛型

  • 部分类型

  • 匿名方法

  • 迭代器

  • 可空类型

  • Getter / setter单独可访问性

  • 方法组转换(代表)

  • Co- and Contra-variance for delegates

  • 静态类

  • Delegate inference

C#3.0
  • 隐式类型局部变量

  • 对象和收集初始化器

  • 自动实现的属性

  • 匿名类型

  • 扩展方法

  • 查询表达式

  • Lambda表达式

  • 表达树

  • 部分方法

C#4.0
  • 动态绑定

  • 命名和可选参数

  • Generic co- and contravariance

  • 嵌入式互操作类型(“NoPIA”)

C#5.0
  • 异步方法

  • Caller info attributes

C#6.0
  • Compiler-as-a-service(Roslyn)

  • 将静态类型成员导入命名空间

  • 异常过滤器

  • 在Catch和Finally中使用Await

  • 自动属性初始化器

  • 只读属性的默认值

  • Expression-bodied members

  • Null-conditional operators(空条件运算符,简洁检查)

  • 字符串插值

  • nameof operator

  • 字典初始化器

C#7.0
  • out变量

  • 模式匹配

  • 元组

  • 解构

  • 局部函数

  • 数字分隔符

  • 二进制文字

  • 局部引用和引用返回

  • 扩展异步返回类型

  • 表达式的构造函数和finalizers

  • Expression bodied getters and setters

  • throw表达式

c#7.0新特性详解

C#7.0增加许多新功能,重点是数据,代码简化和性能上。

Out variables

目前在C#中,使用out参数不像我们想要的那么流畅。在使用out参数调用一个方法之前,首先必须声明变量来传递给它。您也不能使用var它们来声明它们,但需要指定完整的类型。

public void PrintCoordinates(Point p)
{
int x, y; //必须声明
p.GetCoordinates(out x, out y);
WriteLine($"({x}, {y})");
}
  在c#7中
public void PrintCoordinates(Point p)
{
p.GetCoordinates(

out int x, out int y

);
WriteLine($"({x}, {y})");
}
模式匹配

以前版本需要转化

public static void PrintStars(object o)
{
if (o is int) Console.WriteLine(Convert.ToInt32(o) + 12);
}
    在c#7中
public static void PrintStars(object o)
{
if (

o is int i

)
Console.WriteLine(i + 12);
}
Switch statements with patterns 扩展switch语句使用模式匹配

public static void PrintStars(object o)

{

switch (o)

{

case Print p:

break;

case int a:

break;

case String b when b=="123":

break;

}

}

}

元组(Tuples)

元组依赖于一组基础类型,不包括在预览4中。要使功能正常工作,您可以通过NuGet轻松获取它们:

  • 右键单击解决方案资源管理器中的项目,然后选择“管理NuGet软件包...”

  • 选择“浏览”选项卡,选中“包含预发行”,然后选择“nuget.org”作为“包源”

  • 搜索“System.ValueTuple”并安装它。

static void Main(string[] args)

{

var tuple = (a: 10, b: "123");

Console.WriteLine($"a:{tuple.a},b:{tuple.b}");

var result1 = GetS();

var result = Get();

Console.WriteLine($"Item1:{result1.Item1},Item2:{result1.Item2},Item3:{result1.Item3}");

Console.WriteLine($"a:{result.a},b:{result.b},c:{result.c}");

Console.ReadLine();

}

static (string, int, DateTime) GetS()

{

return ("abc", 123, DateTime.Now);

}

static (string a, int b, DateTime c) Get()

{

return (a: "abc", b: 123, c: DateTime.Now);

}

部函数

简单的说,就是在方法里面写方法然后自己调用。

static void Main(string[] args)
{
Console.WriteLine($"{Get(123)},{Get("abc")},{Get(null)}");
Console.ReadLine();
} public static string Get(object a)
{
return GetP();
string GetP()
{
if (a is int v) return v + "";
if (a is string b) return b;
return "ccc";
}
}
Literal improvements

C#7.0允许在数字文字中_作为数字分隔符出现:

var d = 123_456;
var x = 0xAB_CD_EF;

你可以把它们放在数字之间,以提高可读性。它们对价值没有影响。另外,C#7.0引入了二进制文字,因此您可以直接指定位模式,而不必以心脏知道十六进制符号。

var b = 0b1010_1011_1100_1101_1110_1111;
更多c#7.0的特性

请参考文档:https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

c#6.0新语言功能详解

以下新功能在VS 2015及17中实现并可用

字符串格式化

上面的代码中都有体现。

public void Main()
{
  int i = 1;
  string s = "id";
 
  Console.WriteLine($"{s}-{i}");
}

字典索引初始化
var numbers = new Dictionary<int, string> {
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
自动属性初始化
public class Customer
{
public string First { get; set; } = "Jane";
public string Last { get; set; } = "Doe";
}
using引用静态类

If you have a static class, whose members you are using a lot you can now avoid typing the class name everytime by including the class in the using declaration.

using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
WriteLine(Friday - Monday);
}
}
Exception Filters

Exception filters allow you to add additional conditions to exception handlers. We can now write an if a when statement next to a catch block & the catch block will only get executed if the condition returns true. Below is an impractical & silly example to play around with.

public void Main()
{
  try
  {   
      throw new Exception("E2");
  }
  catch(Exception ex) when (ex.Message == "E1")
  {
    Console.WriteLine("caught E1");
  }
  catch(Exception ex) when (ex.Message == "E2")
  {
    Console.WriteLine("caught E2");
  }
}

Using await in catch and finally blocks

We can now await functions in catch and finally blocks. This was not allowed prior to C# 6.0

public void Main()
{
  BuggyFunctionAsync();
  Console.WriteLine("done!");
  Thread.Sleep(2000);
}

public async void BuggyFunctionAsync()
{
  try { throw new Exception(); }
  catch
  {
    Console.WriteLine("entering catch block");
    await Task.Delay(1000);
    Console.WriteLine("exiting catch block");
  }
  finally
  {
    Console.WriteLine("entering finally block");
    await Task.Delay(1000);
    Console.WriteLine("exiting finally block");
  }
}

The nameof Operator

There are times when we need the name of a variable in string form. nameof operator does just that. It takes a variable and converts the variable name to string.

public void Main()
{
  Console.WriteLine(nameof(Account));
  Console.WriteLine(nameof(Account.AccountNumber));
}

class Account
{
  public int AccountNumber{get; set;}
}

Null Conditional Operator

if(node==null || node.Children == null)

Console.WriteLine("Invalid Node");

if(node?.Children == null)

Console.WriteLine("Invalid Code");

c#2.0-5.0参考文档

c#5.0参考文档

连接地址:https://blogs.msdn.microsoft.com/mvpawardprogram/2012/03/26/an-introduction-to-new-features-in-c-5-0/

c#4.0参考文档

连接地址:https://msdn.microsoft.com/en-us/magazine/ff796223.aspx

c#3.0参考文档

连接地址:https://msdn.microsoft.com/en-us/library/bb308966.aspx

c#2.0参考文档

连接地址:https://msdn.microsoft.com/en-us/library/7cz8t42e(v=vs.80).aspx

C#7.0&6.0新特性 — 完整版的更多相关文章

  1. 有史来最大改变 Android 5.0十大新特性

    有史来最大改变 Android 5.0十大新特性 2014.10.16 14:51:31 来源:腾讯数码作者:腾讯数码 ( 0 条评论 )   距离Android系统上一次重大更新不到一年的时间,谷歌 ...

  2. C# 6.0可能的新特性及C#发展历程

    据扯,C# 6.0在不远的将来就发布了,对应的IDE可能是VS 2014(.Net Framework 5.0),因为VS 2013已于2013年10月份发布了,对应的是.Net Franework ...

  3. C# 6.0可能的新特性

    C# 6.0可能的新特性 1.主构造函数(Primary Constructors) 主构造函数给类中的变量赋值 Before public class Point { private int x, ...

  4. Spring Boot 2.0正式发布,新特性解读

    作者|翟永超 Spring Boot 2.0 来啦,有哪些新特性?升级吗? 写在前面 北京时间 3 月 1 日,经过漫长的等待之后,Spring Boot 2.0 正式发布.作为 Spring 生态中 ...

  5. C# 6.0可能的新特性及C#发展历程[转]

      C# 6.0可能的新特性及C#发展历程[转] 年10月份发布了,对应的是.Net Franework 4.5.1. 或者3年,更新增加的东西会比较多,所以对于C# 6.0,还是有一些期待的. 下面 ...

  6. MySQL 8.0.2复制新特性(翻译)

    译者:知数堂星耀队 MySQL 8.0.2复制新特性 MySQL 8 正在变得原来越好,而且这也在我们MySQL复制研发团队引起了一阵热潮.我们一直致力于全面提升MySQL复制,通过引入新的和一些有趣 ...

  7. Atitit.c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结

    Atitit.c# .net 3.5 4.0 各个版本新特性战略规划总结 1. --------------.Net Framework版本同CLR版本的关系1 2. paip.----------- ...

  8. c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结【转载】

    引用:http://blog.csdn.net/attilax/article/details/42014327 c# .net 3.5 4.0 各个版本新特性战略规划总结 1. ---------- ...

  9. 转载——C# 6.0可能的新特性及C#发展历程

    据扯,C# 6.0在不远的将来就发布了,对应的IDE可能是VS 2014(.Net Framework 5.0),因为VS 2013已于2013年10月份发布了,对应的是.Net Franework ...

随机推荐

  1. Memcached 快速入门

    Memcached简介 Memcached是一个专门用来做缓存的服务器,而且缓存的数据都在内存中.Memcached就相当于一个Dictionary键值对集合,保存的是键值对,然后根据key取valu ...

  2. Oracle数据库中字符型字段按数字排序

    今天在转换数据时,遇到了一个主键排序的问题.字符型的主键,保存的都是数字,数据导过来以后发现数据排序都是乱的,就想着按数字规则排序. 但发现to_number总是报错,就想着里面应该是有字符存在.后来 ...

  3. ASP.NET machineKey的作用和使用方法

    ASP.NET machineKey的作用 如果你的Asp.Net程序执行时碰到这种错误:“验证视图状态 MAC 失败.如果此应用程序由网络场或群集承载,请确保 <machineKey> ...

  4. Ant之build.xml详解

    Ant之build.xml详解 关键字: ant build.xml Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译 ...

  5. python基础——函数

    1.内置函数的调用: 可以在官方网站查找内置函数及说明,也可以通过help(abs)函数查看相应的信息. https://docs.python.org/3/library/functions.htm ...

  6. md5爆破工具

    http://www.myhack58.com/Article/html/3/8/2015/65021.htm http://xlixli.net/?p=410 http://blog.csdn.ne ...

  7. 禁止root直接登陆linux系统

    直接修改文件 # vim /etc/ssh/sshd_config SyslogFacility AUTHPRIV PermitRootLogin no RSAAuthentication yes P ...

  8. $on在构造器外部添加事件$once执行一次的事件$off关闭事件

    $on 在构造器外部添加事件. $on接收两个参数,第一个参数是调用时的事件名称,第二个参数是一个匿名方法. 如果按钮在作用域外部,可以利用$emit来执行. html <div id=&quo ...

  9. UOJ#33. 【UR #2】树上GCD 点分治 莫比乌斯反演

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ33.html 题解 首先我们把问题转化成处理一个数组 ans ,其中 ans[i] 表示 d(u,a) 和 ...

  10. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第九集之安装Tomcat+Nginx反向代理Tomcat集群】

    1,安装Tomcat:和在windows下安装是一样的. 安装tomcat:上传后解压: tar -zxvf apache-tomcat-7.0.88.tar.gz -z:代表.gz格式的压缩包,-x ...