.Net5.0,终于江山一统了,看看RC2特性

.Net5.0,终于江山一统了,看看RC2特性

.net5.0只是整合了所有的框架,把之前多个独立的合成一个,这样的好处就是你不用针对某些应用要独立下载某个框架,比如你想开发手机应用需要Xamarin,如果你开发网站需要asp.net 等等,现在只需要.net5.0就可以了。至于在开发上,其实在开发上没有太大的变化,以前怎么开发就怎么开发,函数,类名没有太多的变化。

.net5.0将在今年2020年11月发布,到时候不再取名.net core,而是.net5.0,也就是只有这么一个版本,到时候这个版本将会统一所有平台,微软将会把NET Core、.NET Framework、Xamarin、mono整合起来,在这上面可以开发android、ios,winform、UPW、WPF等各种应用程序。

.net 5不仅可以在windows开发,也可以在linux、mac os系统等开发。相关的开发工具有Visual Studio、VS for Mac、VS Code、CMD等

 

Today(10.13), we are shipping .NET 5.0 Release Candidate 2 (RC2). It is a near-final release of .NET 5.0, and the last of two RCs before the official release in November. RC2 is a “go live” release; you are supported using it in production. At this point, we’re looking for reports of any remaining critical bugs that should be fixed before the final release.

今天(10月13日)我们发布了 .NET 5.0 候选版本 2(RC2),它是 11 月 .NET 5.0 正式版发布前的最后一个 RC 版本。RC2 是一个“上线”版本,表示你可以在生产环境中使用它。与此同时,我们正在找寻任何遗漏的关键 Bug 报告,这些 Bug 应该在最终发布前得到修复。

We also released new versions of ASP.NET Core and EF Core today.

You can download .NET 5.0, for Windows, macOS, and Linux:

  • Installers and binaries
  • Container images
  • Snap installer
  • Release notes
  • Known issues
  • GitHub issue tracker

You need the latest preview version of Visual Studio (including Visual Studio for Mac) to use .NET 5.0.

.NET 5.0 includes many improvements, notably single file applications, smaller container images, more capable JsonSerializer APIs, a complete set of nullable reference type annotations, new target framework names, and support for Windows ARM64. Performance has been greatly improved, in the NET libraries, in the GC, and the JIT. ARM64 was a key focus for performance investment, resulting in much better throughput and smaller binaries. .NET 5.0 includes new language versions, C# 9 and F# 5.0. Check out some .NET 5.0 examples so you can try these features out for yourself.

.NET 5.0 包括许多改进,特别是单文件应用程序、更小的容器镜像、功能更强的 JsonSerializer API、一套完整的可空引用类型标注、新的Target Framework 名称,以及对 Windows ARM64 的支持。在 .NET 库、GC 和 JIT 中,性能得到了极大的提高。我们花了很大的工作在 ARM64 的性能上,它有了更好的吞吐量和更小的二进制文件。.NET 5.0 包含了新的语言版本:C# 9.0 和 F# 5.0。你可以查看一些 .NET 5.0 示例,以便你可以亲自尝试这些功能。

Today is an auspicious day because we’re kicking off the 2020 .NET@Microsoft internal conference. There will be many speakers from the .NET team, but also developers and architects from services teams that rely on .NET to power the Microsoft cloud, sharing their victories and also their challenges. I’m presenting (unsurprisingly) “What’s new in .NET 5.0”. My talk will be easy; I’ll just read the .NET 5.0 blog posts, preview by preview! It will be a great talk. More seriously, the conference is our opportunity to make the case why Microsoft teams should adopt .NET 5.0 soon after it is available. At least one large team I know of is running on RC1 in production. The official .NET Microsoft site has been running on .NET 5.0 since Preview 1. It is now running RC2. The case we’ll make to Microsoft teams this week is very similar to the case that I’ve intended to make to you across all of these .NET 5.0 blog posts. .NET 5.0 is a great release and will improve the fundamentals of your app.

Speaking of conferences, please save the date for .NET Conf 2020. This year, .NET 5.0 will launch at .NET Conf 2020! Come celebrate and learn about the new release. We’re also celebrating our 10th anniversary and we’re working on a few more surprises. You won’t want to miss this one.

说到会议,请记住 .NET Conf 2020 的日期(译注:11月10日至12日)。今年,.NET 5.0 将在 .NET Conf 2020 上发布!请来庆祝并了解新版本。我们还将庆祝我们的 10 周年纪念日,我们正在努力创造更多的惊喜,值得你的期待。

Just like I did for .NET 5.0 Preview 8 and .NET 5.0 RC1, I’ve chosen a selection of features to look at in more depth and to give you a sense of how you’ll use them in real-world usage. This post is dedicated to C# 9 pattern matching, Windows ARM64, and ClickOnce.

就像我为 .NET 5.0 Preview 8 和 .NET 5.0 RC1 所做的那样,我选择了一些功能来更深入地介绍,并让你了解如何在实际情况中使用它们。这篇文章专门介绍 C# 9 模式匹配。

C# 9 Pattern Matching

Pattern matching is a language feature was first added in C# 7.0. It’s best to let Mads reintroduce the concept. This is what he had to say when he originally introduced the feature.

C# 7.0 introduces the notion of patterns, which, abstractly speaking, are syntactic elements that can test that a value has a certain “shape”, and extract information from the value when it does.

That’s a really great description, perfectly worded.

The C# team has added new patterns in each of the C# 7, C# 8, and C# 9 versions. In this post, you’ll see patterns from each of those language versions, but we’ll focus on the new patterns in C# 9.

The three new patterns in C# 9 are:

  • Relational patterns, using relational operators such as < and >=.
  • Logical patterns, using the keywords andor, and not. The poster child example is foo is not null. This type of pattern is most useful when you want to compare multiple things in one pattern.
  • Simple type patterns, using solely a type and no other syntax for matching.

I’m a big fan of the BBC Sherlock series. I’ve written a small app that determines if a given character should have access to a given piece of content within that series. Easy enough. The app is written with two constraints: stay true to the show timeline and characters, and be a great demonstration of patterns. If anything, I suspect I’ve failed most on the second constraint. You’ll find a broader set of patterns and styles than one would expect in a given app (particularly such a small one).

When I’m using patterns, I sometimes want to do something subtly different than a pattern I’m familiar with achieves and am not sure how to extend that pattern to satisfy my goal. Given this sample, I’m hoping you’ll discover more approaches than perhaps you were aware of before, and can extend your repertoire of familiar patterns.

There are two switch expressions within the app. Let’s start with the smaller of the two.

public static bool IsAccessOKAskMycroft(Person person) => person switch
 {
        // Type pattern
        OpenCaseFile f when f.Name == "Jim Moriarty"    => true,
        // Simple type pattern
        Mycroft                                         => true,
        _                                               => false,
};

The first two patterns are type patterns. The first pattern is supported with C# 8. The second one — Mycroft — is an example of the new simple type pattern. With C# 8, this pattern would require an identifier, much like the first pattern, or at the very least a discard such as Mycroft _. In C# 9, the identifier is no longer needed. Yes, Mycroft is a type in the app.

Let’s keep to simple a little longer, before I show you the other switch expression. The following if statement demonstrates a logical pattern, preceded by two instances of a type pattern using is.

if (user is Mycroft m && m.CaresAbout is not object)
 {
    Console.WriteLine("Mycroft dissapoints us again.");
 }

The type isn’t known, so the user variable is tested for the Mycroft type and is then assigned to m if that test passes. A property on the Mycroft object is tested to be not an object. A test for null would have also worked, but wouldn’t have demonstrated a logical pattern.

The other switch expression is a lot more expansive.

public static bool IsAccessOkOfficial(Person user, Content content, int season) => (user, content, season) switch 
    {
        // Tuple + property patterns
        ({Type: Child}, {Type: ChildsPlay}, _)          => true,
        ({Type: Child}, _, _)                           => false,
        (_ , {Type: Public}, _)                         => true,
        ({Type: Monarch}, {Type: ForHerEyesOnly}, _)    => true,
        // Tuple + type patterns
        (OpenCaseFile f, {Type: ChildsPlay}, 4) when f.Name == "Sherlock Holmes"  => true,
        // Property and type patterns
        {Item1: OpenCaseFile {Type: var type}, Item2: {Name: var name}} 
            when type == PoorlyDefined && name.Contains("Sherrinford") && season >= 3 => true,
        // Tuple and type patterns
        (OpenCaseFile, var c, 4) when c.Name.Contains("Sherrinford")              => true,
        // Tuple, Type, Property and logical patterns 
        (OpenCaseFile {RiskLevel: >50 and <100 }, {Type: StateSecret}, 3) => true,
        _                                               => false,
    };

The only really interesting pattern is the very last one (before the discard: -), which tests for a Risklevel that is >50 and <100. There are many times I’ve wanted to write an if statement with that form of logical pattern syntax without needing to repeat a variable name. This logical pattern could also have been written in the following way instead and would have more closely matched the syntax demonstrated in the C# 9 blog post. They are equivalent.

(OpenCaseFile {RiskLevel: var riskLevel}, {Type: StateSecret}, 3) when riskLevel switch
      {
         >50 and <100        => true,
         _                   => false
      }                                           => true,

I’m far from a language expert. Jared Parsons and Andy Gocke gave me a lot of help with this section of the post. Thanks! The key stumbling block I had was with a switch on a tuple. At times, the positional pattern is inconvenient, and you only want to address one part of the tuple. That’s where the property pattern comes in, as you can see in the following code.

{Item1: OpenCaseFile {Type: var type}, Item2: {Name: var name}} 
            when type == PoorlyDefined && name.Contains("Sherrinford") && season >= 3 => true,

There is a fair bit going on there. The key point is that the tuple properties are being tested, as opposed to matching a tuple positionally. That approaches provides a lot more flexibility. You are free to intermix these approaches within a given switch expression. Hopefully that helps someone. It helped me.

If you are curious about what the app does, I’ve saved the output of the program in the app gist. You can also run the app for yourself. I believe it requires .NET 5.0 RC2 to run.

If there has been a pattern with the last three C# (major) versions, it has been patterns. I certainly hope the C# team matches that pattern going forward. I imagine it is the shape of things, and there are certainly more values to extract.

Closing

We’re now so close to finishing off this release, and sending it out for broad production use. We believe it is ready. The production use that it is already getting at Microsoft brings us a lot of confidence. We’re looking forward to you getting the chance to really take advantage of .NET 5.0 in your own environment.

我们现在非常接近完成 5.0 这个版本,并已将其传递出去以供广泛的生产使用。我们相信它已经准备好了。它在微软的生产环境中使用,已经给我们带来了很大的信心。我们期待你有机会在自己的环境中真正使用 .NET 5.0。

 

更多语法请阅读:https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-5-release-candidate-2/

 

关于C#6.0以后各个版本新增功能,可以移步微软官方:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-6