.Net 5.0 preview

本文参考自:

https://devblogs.microsoft.com/dotnet/announcing-net-5-0-preview-6/

Today, we’re releasing .NET 5.0 Preview 6. It contains a small set of new features and performance improvements. The .NET 5.0 Preview 4 post covers what we are planning to deliver with .NET 5.0. Most of the features are now in the product, but some are not yet in their final state. We expect that the release will be feature-complete with Preview 8.

下载:

如果喜欢使用 vs code 体验的同学可以下载最新版本的 C# 插件:

Top-level programs

Before:

using System;

namespace csharp_9_demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

after:

System.Console.WriteLine("Hello World!");

这个功能类似于 python 语法, 在 py 文件中写入一行 print, 就可以运行, 很简单和直接:

Partial Methods

说实话 Partial Methods, 我在实际项目中几乎很少用到, 所以这里也就将 demo 程序贴出来, 知道怎么个用法就行了. 想了解更详细的内容的话, 建议移步到官方文档: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

Native ints

csharp 9 新增的关键词, 还是先从 demo 看一下运行效果:

f12 进入 nint:

//
    // Summary:
    //     A platform-specific type that is used to represent a pointer or a handle.
    public readonly struct IntPtr : IComparable, IComparable<IntPtr>, IEquatable<IntPtr>, IFormattable, ISerializable
  • 属于 struct 类型

Pattern matching improvements

还是直接看 demo:

static bool OldFunction(string input1, string input2, string input3)
{
    if (input1 == "input1")
    {
        if (input2 == "input2")
        {
            return true;
        }
        else if(input3 == "input3")
        {
            return true;
        }

        return false;
    }
    else
    {
        if (input2 == "input22")
        {
            return true;
        }
        else if (input3 == "input33")
        {
            return true;
        }

        return false;
    }
}

新的写法可以为:

static bool NewFunction(string input1, string input2, string input3) 
{
    return (input1, input2, input3) switch
    {
        ("input1", "input2", _) => true,
        ("input1", _, "input3") => true,
        (_, "input22", _) => true,
        (_, _, "input33") => true,
        _ => false
    };
}

再看看运行效果:

Pattern matching improvements (模式匹配) 写法上精简了很多, 也比较优雅.

Target-typed new

这个新特性可以在 new 对象的时候省略对象的名字

Test test = new Test("nick", "pwd");

Test test2 = new("nick", "pwd");

Lambda discard parameters

省略在 lambda 表达式中的参数:

Func<int, int, bool> func = (_, _) =>
{
    return true;
};

var result = func(10, 20);

to-be continued

猜你喜欢

转载自www.cnblogs.com/it-dennis/p/13204426.html