zoukankan      html  css  js  c++  java
  • 尝试C# 9.0新特性

    仅初始化属性 init-only

    init 关键字,可以在类创建的时候赋值一次,如下代码所示:

        public class InitOnlyClass
        {
            public string FirstName { get; init; }
    
            public string LastName { get; init; }
        }
    
        static void Main(string[] args)
        {
            InitOnlyClass initOnlyClass = new InitOnlyClass
            {
                FirstName = "firstName"
            };
    
            System.Console.WriteLine(initOnlyClass.FirstName);
    
            // 这样会报错。
            // initOnlyClass.LastName = "lastName";
        }

    此种方式只是多了一种构造函数的实例的方式

    record 关键字

    实际上是一个继承了IEquatable<Data>的类,个人理解为一个类似dto的类,只用于记录值。 这个类只用于传输数据,比如用来json序列化。

    语法

    public record Data
    {
        public string Const { get; init; }
        public string Content { get; init; }
    }

    with 关键字

    如果需要更改Data数据只能复制一个,然后修改特定的值即可。如代码所示:

    var data = new Data { Const = "固定内容", Content = "旧内容" }
    
    // 这是要新修改的内容,其余内容将会直接复制。
    var newData = data with { content = "新的内容" };
    var jsonResult = JsonSerializer.Serialize(newData);

    位置记录,实现Deconstruct方法

        public record Data
        {
            private int _id;
            private string _content;
    
            public Data(int id, string content, bool d = false)
            => (_id, _content) = (id, content);
    
            public void Deconstruct(out int id, out string content)
             => (id, content) = (_id, _content);
        }
    
        var data = new Data(1, "发送的内容");
    
        // 获得数据里面的内容,必须实现Deconstruct 方法。
        var (d, c) = data;

    更加简单的实现Data的方式

        // 已经实现Deconstruct。
        public record DataSimple(string Id, string Content);
        var (d, c) = data;

    顶级程序

    直接在program中写逻辑,比如简单的表达式,除了需要(用到时)引入命名空间,有点像写PowerShell

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

    匹配模式 swith C# 8.0的加强版

    using InitOnlyDemo;
    
    // 最简单的匹配程序
    int tag = -1;
    int result = -1;
    
    // 最基本的。
    switch (tag)
    {
        case 0:
            result = 1;
            break;
    
        case 1:
            result = 2;
            break;
    
        case 2:
            result = 3;
            break;
    
        default:
            result = 4;
            break;
    }
    
    System.Console.WriteLine(result);
    
    // C# 8.0。
    tag = -1;
    result = -1;
    result = tag switch
    {
        -1 when result == -1 => 1, // 当tag等于-2时,也返回1。
        0 => 1,
        1 => 2,
        3 => 3,
        _ => 4,
    };
    
    System.Console.WriteLine($"C# 8 => {result}");
    
    // C# 9.0 加入关系模式
    tag = 0;
    result = -3;
    result = tag switch
    {
        -1 when result > -1 => result switch  // 在嵌套一个。
        {
            _ => 0
        },
        0 when result <= -1 => result switch
        {
            < -2 and > -4 => 1,  // 逻辑模式,可用用and or not 判断
            _ => 0
        },
        1 => 2,
        3 => 3,
        _ => 4,
    };
    
    System.Console.WriteLine($"C# 9 => {result}");

    自动推断类型

    在new对象十分好用。但当需要引入新命名空间才能使用的对象时(通常在接收返回值),还是var xx=x.GetX(); 这种类型好用。
    
    var data8 = new Data(1, "需要的内容");
    
    // C# 9.0。
    Data data9 = new(1, "需要的内容");
    A a = new() { };
  • 相关阅读:
    Spring事务传播特性NOT_SUPPORTED使用演示
    spring配置基于xml的声明式事务
    反射的简单应用
    MyBatis批处理工具类MyBatisBatchHelper.java
    Mybatis分页插件PageHelper的使用
    利用Spring的Profile加载指定数据源
    Nginx状态信息(status)配置及信息详解
    nginx配置基于域名、端口、IP的虚拟主机
    Nginx Linux安装与部署
    rename 批量修改文件名简单用法
  • 原文地址:https://www.cnblogs.com/yeqifeng2288/p/13582857.html
Copyright © 2011-2022 走看看