• dotnet record


    本质上record是一个实现了Equals()/GetHashCode()和ToString()的class

        record Person(int Id, string Name, int Age)  // 这3个都是只读属性
        {
            public string NickName { get; set; }   // 可写属性
    
            void Play()
            {
                Console.WriteLine($"ppp:{Name}");
            }
        }
    
          // 
          Person p1 = new Person(1, "p1", 11) { NickName = "n1" };
          Person p2 = new Person(1, "p1", 11) { NickName = "n2" };
    
          // false,因为NickName也参与比较
          Console.WriteLine(p1 == p2);
    
          // 使用with语法,可以复制record,再改一些属性。
          Person p3 = p1 with { };
          Console.WriteLine(p1 == p3);
    
          Person p4 = p1 with { Age = 12 };
          Console.WriteLine(p1 == p4);
    
  • 相关阅读:
    zabbix入门知识
    flask_login
    flask_数据库
    flask_web表单
    flask_模板
    flask_hello world
    1024 Hello World
    使用bat批处理文件备份postgresql数据库
    使用bat批处理文件备份mysql数据库
    在windows7下创建ftp服务站点
  • 原文地址:https://www.cnblogs.com/mryux/p/15863855.html
Copyright © 2020-2023  润新知