序言
语言版本 | 发布时间 | .NET Framework要求 |
C# 1.0 | 2002.1 | .NET Framework 1.0 |
C# 1.11.2 | 2003.4 | .NET Framework 1.1 |
C# 2.0 | 2005.11 | .NET Framework 2.0 |
C# 3.0 | 2007.11 | .NET Framework 2.03.03.5 |
C# 4.0 | 2010.4 | .NET Framework 4.0 |
C# 5.0 | 2012.8 | .NET Framework 4.5 |
C# 6.0 | 2015.7 | .NET Framework 4.6 |
C# 7.0 | 2017.3 | .NET Framework 4.6.2 |
C# 7.1 | 2017.6 | .NET Framework 4.7 |
C# 7.2 | 2017.11 | .NET Framework 4.7.1 |
C# 7.3 | 2018.5 | .NET Framework 4.7.2 |
C# 8.0 | 2019.4 | .NET Framework 4.8 |
C# 6.0 (2015)
静态导入
异常筛选器
自动属性初始化表达式
Expression bodied 成员
Null 传播器
字符串内插
nameof 运算符
索引初始值设定项
字典初始化
class StudentName { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } } class CollInit { Dictionary<int, StudentName> students = new Dictionary<int, StudentName>() { { 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}}, { 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}}, { 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}} }; }
异常过滤器
try { } catch (Exception) { } finally { }
表达式体成员
nameof 操作符
Console.WriteLine(nameof(Int32));
空合并运算符
??被称为【可空合并】运算符,是一个二目运算符,操作参数两枚,其完成的功能为对左参数判断之后进行赋值。
返回结果:如果左操作数不为空,则返回左操作数本身;如果左操作书为空,则返回右操作数。
格式:object1 ?? object2
int? x = null; int y = x ?? -1; Console.WriteLine(string.Format("x={0},y={1}", x, y));
属性初始化
class Person { public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } } Person person = new Person { Name = "Slark", Age = 100, Address = "Xi'an" }; List<int> intList = new List<int> { 1, 2, 3 }; List<Person> personList = new List<Person> { new Person { Name = "Slark1", Age = 101, Address = "Xi'an1" }, new Person { Name = "Slark2", Age = 102, Address = "Xi'an2" }, new Person { Name = "Slark3", Age = 103, Address = "Xi'an3" } };
静态引用
字符串插值
编程中经常用到字符串的拼接。
class Plane { public string Category { get; set; } public string Name { get; set; } public int State { get; set; } }
C#6.0之前的方法:
var p = new Plane() { Category = "A", Name = "B", State = 0 }; var sb=new StringBuilder(); sb.Append(string.Format("Category={0},Name={1},State={2}",plan.Category,plane.Name,plane.State);
C#6.0之后的方法:
var p = new Plane() { Category = "A", Name = "B", State = 0 }; var sb=new StringBuilder(); sb.Append($"Category={p.Category},Name={p.Name},State={p.State}");
C# 7.0 (2017)
Out 变量
元组和析构函数
模式匹配
本地函数
已扩展 expression bodied 成员
Ref 局部变量和返回结果
其他功能包括:
弃元
二进制文本和数字分隔符
引发表达式
重点是缩减了使用 out
关键字的变量声明,并通过元组实现了多个返回值。
表达式体成员
局部方法
Out 变量
模式匹配
static dynamic Add(object a) { dynamic data; switch (a) { case int b: data=b++; break; case string c: data= c + "aaa"; break; default: data = null; break; } return data; }
匹配模式的Case When筛选
switch (a) { case int b when b < 0: data = b + 100; break; case int b: data=b++; break; case string c: data= c + "aaa"; break; default: data = null; break; }
局部引用和引用返回
public static void DoSomeing() { //调用Dosmeing2 int data = Dosmeing2(100, 200); Console.WriteLine(data); //定义局部函数,Dosmeing2. int Dosmeing2(int a, int b) { return a + b; } }
元组
static void Main(string[] args) { var data = GetFullName(); Console.WriteLine(data.Item1); Console.WriteLine(data.Item2); Console.WriteLine(data.Item3); Console.ReadLine(); } static Tuple<string, string, string> GetFullName() { return new Tuple<string, string, string>("a", "b", "c"); }
解构元组
有的时候我们不想用var匿名来获取,那么如何获取abc呢?我们可以如下:
static void Main(string[] args) { //定义解构元组 (string a, string b, string c) = GetFullName(); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.ReadLine(); } private static (string a,string b,string c) GetFullName() { return ("a","b","c"); }
C# 8.0(2019)
资料
https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-version-history
https://www.cnblogs.com/microsoft-zyl/p/9093748.html