近日,微软发布了Visual Studio 2019 的发布日期,2019年4月2日Visual Studio 2019 将正式和大家见面,同时微软还将提供发布现场实时直播。
\n除了Visual Studio 2019自身之外,VS 2019的发布还牵动着很多C#开发者的心。虽然一个月之前发布的Visual Studio 2019 Preview版本已经可以试用C#的某些新功能,但还有一些是不可试用的。
\n下面我们就来看一下微软官方对C#8.0重要功能的概述。
\n可空的引用类型
\n此功能的目的是防止无处不在的空引用异常,空引用异常已经困扰面向对象编程半个世纪了。该功能将阻止开放者将null值放入到普通的引用类型中,例如String类型不可为空。但它不是强制性的error,而是比较温和的warning。
\n这些异常现在已经过了半个世纪的面向对象编程。
\n它阻止你null进入普通的引用类型,例如string- 它使这些类型不可为空!它是温和的,有警告,而不是错误。但是在现有代码上会出现新警告,因此您必须选择使用该功能(您可以在项目,文件甚至源代码级别执行此功能)。
\nstring s = null; // Warning: Assignment of null to non-nullable reference type
\n如果你想要使用null怎么办?可以使用可为空的引用类型,例如string?:
\nstring? s = null; // Ok
\n当你使用了可空引用时,需要先检查一下其是否为null,编译器会分析代码流,以查看null值是否可以将其用于您使用它的位置:
void M(string? s)\n{\n Console.WriteLine(s.Length); // Warning: Possible null reference exception\n if (s != null)\n {\n Console.WriteLine(s.Length); // Ok: You won't get here if s is null\n }\n}\n
\n
C#允许表达可空的意图,但是在不遵守规则时会发出警告。
\n异步流
\nC#5.0的async / await功能允许在简单的代码中使用(并生成)异步结果,而无需回调:
\nasync Task\u0026lt;int\u0026gt; GetBigResultAsync()\n{\n var result = await GetResultAsync();\n if (result \u0026gt; 20) return result; \n else return -1;\n}\n
\n
下面我们来介绍一下大家期待已久的IAsyncEnumerable, 异步版本的IEnumerable。该语言允许await foreach使用元素,并使用yield return生成元素。
\nasync IAsyncEnumerable\u0026lt;int\u0026gt; GetBigResultsAsync()\n{\n await foreach (var result in GetResultsAsync())\n {\n if (result \u0026gt; 20) yield return result; \n }\n}\n
\n
范围和索引
\n我们正在添加一个可用于索引的Index类型。你可以使用int从头创建,也可以使用^从末尾开始计算前缀运算符:
\nIndex i1 = 3; // number 3 from beginning
\nIndex i2 = ^4; // number 4 from end
\nint[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
\nConsole.WriteLine($\u0026quot;{a[i1]}, {a[i2]}\u0026quot;); // “3, 6”
\n另外,我们还引入了一个Range类型,它由两个Indexes 组成,一个用于开始,一个用于结束,并且可以用x…y 范围表达式编写。
\n可以使用a进行索引Range以生成切片:
\nvar slice = a[i1…i2]; // { 3, 4, 5 }
接口成员的默认实现
\n今天,大家对于界面都有这样一个需求:在不破坏现有状态的情况下添加一个成员。
\n在C#8.0中,我们会为接口成员提供一个主体。如果有人没有实现该成员(或者是在编写代码时还没有实现),会获得默认实现。
\ninterface ILogger\n{\n void Log(LogLevel level, string message);\n void Log(Exception ex) =\u0026gt; Log(LogLevel.Error, ex.ToString()); // New overload\n}\n\nclass ConsoleLogger : ILogger\n{\n public void Log(LogLevel level, string message) { ... }\n // Log(Exception) gets default implementation\n}\n\n
\n
在ConsoleLogger类不需要实现ILogger的Log(Exception)重载,因为它已经默认实现了。现在只要给当前实现者提供了默认实现,就可以向现有公共接口添加新成员。
\n递归模式
\n我们允许pattern中包含其他pattern:
\nIEnumerable\u0026lt;string\u0026gt; GetEnrollees()\n{\n foreach (var p in People)\n {\n if (p is Student { Graduated: false, Name: string name }) yield return name;\n }\n}\n
\n
pattern Student { Graduated: false, Name: string name }主要检查Person是a Student,然后将常量pattern false应用于其Graduated属性以查看它们是否仍然已注册,并将pattern string name应用于其Name属性以获取其名称(如果为非null)。因此,如果p是一个Student,尚未毕业并且姓名非空,那么我们就可以yield return这个名字。
\nSwitch表达式
\n带有pattern的switch语句在C#7.0中已经非常强大了,但是编写起来却很麻烦,而Switch 表达式却是一个解决这种问题的、“轻量级”的版本。
\nvar area = figure switch \n{\n Line _ =\u0026gt; 0,\n Rectangle r =\u0026gt; r.Width * r.Height,\n Circle c =\u0026gt; Math.PI * c.Radius * c.Radius,\n _ =\u0026gt; throw new UnknownFigureException(figure)\n};\n
\n
目标类型的新表达式
\n在许多情况下,往往创建新对象时,类型已经从上下文中给出。在这些情况下,我们会让你省略类型:
\nPoint[] ps = { new (1, 4), new (3,-2), new (9, 5) }; // all Points
C#大版本关键更新回顾
\nC#1.0(Visual Studio .NET)
\n- \n
- Classes\n
- Structs\n
- Interfaces\n
- Events\n
- Properties\n
- Delegates\n
- Expressions\n
- Statements\n
- Attributes\n
- Literal\n
C#2(VS 2005)
\n- \n
- Generics\n
- Partial types\n
- Anonymous methods\n
- Iterators\n
- Nullable types\n
- Getter/setter separate accessibility\n
- Method group conversions (delegates)\n
- Static classes\n
- Delegate inferenc\n
C#3(VS 2008)
\n- \n
- Implicitly typed local variables\n
- Object and collection initializers\n
- Auto-Implemented properties\n
- Anonymous types\n
- Extension methods\n
- Query expressions\n
- Lambda expression\n
- Expression trees\n
- Partial methods\n
C#4(VS 2010)
\n- \n
- Dynamic binding\n
- Named and optional arguments\n
- Co- and Contra-variance for generic delegates and interfaces\n
- Embedded interop types (“NoPIA”\n
C#5(VS 2012)
\n- \n
- Asynchronous methods\n
- Caller info attributes\n
C#6(VS 2015)
\n- \n
- Draft Specification online\n
- Compiler-as-a-service (Roslyn)\n
- Import of static type members into namespace\n
- Exception filters\n
- Await in catch/finally blocks\n
- Auto property initializers\n
- Default values for getter-only properties\n
- Expression-bodied members\n
- Null propagator (null-conditional operator, succinct null checking)\n
- String interpolation\n
- nameof operator\n
- Dictionary initializer\n
C#7.0(Visual Studio 2017)
\n- \n
- Out variables\n
- Pattern matching\n
- Tuples\n
- Deconstruction\n
- Discards\n
- Local Functions\n
- Binary Literals\n
- Digit Separators\n
- Ref returns and locals\n
- Generalized async return types\n
- More expression-bodied members\n
- Throw expressions\n
平台依赖
\n大多数的C# 8.0功能都可以在任何版本的.NET上运行,但也有一些功能是有平台依赖性的,例如异步流、范围和索引都依赖 .NET Standard 2.1一部分的新框架类型。其中,.NET Standard 2.1、.NET Core 3.0以及Xamarin,Unity和Mono都将实现 .NET Standard 2.1,而.NET Framework 4.8不会,所以如果你使用的是 .NET Framework 4.8,那么C# 8.0的部分功能可能不能使用。
\n另外,接口成员的默认实现也依赖新的运行时增强功能,所以此功能也不适用于 .NET Framework 4.8和旧版本的 .NET。
\n微软官方博客链接:https://blogs.msdn.microsoft.com/dotnet/2018/11/12/building-c-8-0/
\n