\u4e3a\u4e86\u65b9\u4fbf\u6f14\u793a\uff0c\u6211\u5148\u4e0a\u4e00\u6bb5\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
\r\npublic record Person { public string Name { get; set; } public int Age { get; set; } }\r\n<\/pre>\n\u770b\u8d77\u6765\u633a 🐂👃 \u7684\uff0c\u73b0\u5728\u9664\u4e86 class\uff0cstruct , enum, delegate\uff0c\u53c8\u6765\u4e86\u4e00\u4e2a record\uff0c\u4ffa\u4eec\u7684 C# \u662f\u8d8a\u6765\u8d8a\u5f3a\u5927\u5566\u3002<\/p>\n
\u8fd8\u662f\u8001\u89c4\u77e9\uff0c\u7528ILspy\u770b\u770b\u5e95\u5c42\u751f\u6210\u4e86\u4e2a\u5565\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n
\r\npublic class Person : IEquatable { protected virtual Type EqualityContract => typeof(Person); public string Name { get; set; } public int Age { get; set; } public virtual Person <>Clone() { return new Person(this); } public override int GetHashCode() { return (EqualityComparer.Default.GetHashCode(EqualityContract) * -1521134295 + EqualityComparer.Default.GetHashCode(Name)) * -1521134295 + EqualityComparer.Default.GetHashCode(Age); } public override bool Equals(object? obj) { return Equals(obj as Person); } public virtual bool Equals(Person? P_0) { return P_0 != null && (object)EqualityContract == P_0!.EqualityContract && EqualityComparer.Default.Equals(Name, P_0!.Name) && EqualityComparer.Default.Equals(Age, P_0!.Age); } protected Person(Person P_0) { Name = P_0.Name; Age = P_0.Age; } public Person() { } bool IEquatable .Equals(Person other) { return Equals(other); }}\r\n<\/pre>\n\u4ece ILspy \u751f\u6210\u51fa\u6765\u7684\u4ee3\u7801\u6765\u770b\uff0c\u53ef\u4ee5\u53d1\u73b0\u4e24\u70b9\u4fe1\u606f\uff1a<\/p>\n
record \u73a9\u7684\u4e5f\u662f class\uff0c\u91cd\u5199\u4e86 object \u4e2d\u7684\u4e00\u4e9b\u65b9\u6cd5 GetHashCode, Equals \u7b49\u7b49\u3002<\/p>\n
\u6309\u7c7b\u4e2d\u7684\u5b57\u6bb5\u9010\u4e00\u6bd4\u8f83\u5224\u65ad\u7c7b\u7684\u76f8\u7b49\u6027\u3002<\/p>\n
\u8bf4\u5230\u6839\u636e\u5b57\u6bb5\u5224\u65ad\u7c7b\u7684\u76f8\u7b49\u6027\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u53ef\u6709\u4f3c\u66fe\u76f8\u8bc6\u7684\u611f\u89c9? ,\u53cd\u6b63\u8ba9\u6211\u60f3\u8d77\u4e86\u533f\u540d\u7c7b\u578b\uff0c\u56e0\u4e3a\u5b83\u751f\u6210\u7684 C# \u4ee3\u7801\u548c record \u5982\u51fa\u4e00\u8f99\uff0c\u4e0d\u4fe1\u7684\u8bdd\uff0c\u6211\u6f14\u793a\u7ed9\u4f60\u770b\u5457\u3002<\/p>\n
var person = new { Name = \"jack\", Age = 20 };
\n
\n\u63a5\u4e0b\u6765\u770b\u4e00\u770b\u662f\u5426\u771f\u7684\u662f\u6309\u7167\u9010\u4e00\u5b57\u6bb5\u6bd4\u8f83\uff0c\u4ee3\u7801\u5982\u4e0b\u56fe\uff1a<\/p>\n
\r\nstatic void Main(string[] args) { var person = new Person() { Name = \"jack\", Age = 20 }; var person2 = new Person() { Name = \"jack\", Age = 20 }; var b = person.Equals(person2); }\r\n<\/pre>\n
\n\u770b\u4e86\u8fd9\u4e48\u591a\uff0c\u6211\u60f3\u4f60\u80af\u5b9a\u6709\u4e00\u4e9b\u7591\u95ee\uff1a<\/p>\n
1) \u4e3a\u5565\u8981\u5b9e\u73b0 IEquatable \u63a5\u53e3<\/p>\n
\u8fd9\u662f\u56e0\u4e3a\u5728\u5f53 Person \u662f \u6cdb\u578b T \u7684\u65f6\u5019\u907f\u514d\u8d70\u4e86\u9ed8\u8ba4\u7684 public override bool Equals(object? obj)\uff0c\u8fd9\u662f\u4e00\u4e2a\u53cc\u88c5\u7bb1\u64cd\u4f5c\uff0c\u6027\u80fd\u592a\u4f4e\u6548\uff0c\u6df1\u5165\u7814\u7a76\u53ef\u770b\u6211\u7684\u535a\u6587\uff1ahttps:\/\/www.cnblogs.com\/huangxincheng\/p\/12996361.html \u3002<\/p>\n
2) \u4e3a\u5565\u6709 equals \u6ca1\u6709 ==<\/p>\n
\u8fd9\u4e2a\u95ee\u9898\u95ee\u5f97\u597d\uff0c\u8c01\u77e5\u9053 C# \u5f00\u53d1\u56e2\u961f\u600e\u4e48\u60f3\u7684\uff0c\u6309\u7167\u76ee\u524d\u73b0\u72b6\uff0c \u7528 == \u548c equals \u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\uff0c\u7ed3\u679c\u80af\u5b9a\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u6211\u60f3\u4f60\u80af\u5b9a\u80fd\u7406\u89e3\uff0c\u6bd5\u7adf\u4e00\u4e2a\u662f\u5f15\u7528\u4e00\u4e2a\u662f\u6309\u5b57\u6bb5\u6bd4\u8f83\uff0c\u8fd9\u5c31\u6bd4\u8f83\u5751\u7239\u4e86\uff0c\u5982\u4e0b\u56fe\uff1a
\n
\n3) <>Clone() \u65b9\u6cd5\u6709\u4f55\u4f5c\u7528<\/p>\n
\u4ece\u65b9\u6cd5\u4f53\u6765\u770b\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u7528\u4e8e\u505a \u6d45copy \u7528\u7684\uff0c\u4f46\u65b9\u6cd5\u540d\u524d\u9762\u6709\u4e00\u5bf9 <> \uff0c\u8bf4\u660e\u662f\u9632\u4f60\u76f4\u63a5\u8c03\u7528\u7684\uff0c\u90a3\u95ee\u9898\u6765\u4e86\uff0c\u600e\u4e48\u8c03\u7528\u5462?\u8fd9\u5c31\u6d89\u53ca\u4e00\u4e2a\u65b0\u7684\u8bed\u6cd5\u7cd6\u3002<\/p>\n
3. with<\/strong><\/span><\/div>\n\u8fd9\u4e2a\u8bed\u6cd5\u7cd6\u4e5f\u633a🐂👃\u7684\uff0c\u5c31\u662f\u4e3a\u4e86\u52a9\u4f60\u8c03\u7528 record \u7684 <>clone \u65b9\u6cd5\uff0c\u4e0d\u4fe1\u7684\u8bdd\uff0c\u4e0a\u4ee3\u7801\u5457\u3002<\/p>\n
static void Main(string[] args) { var person = new Person() { Name = \"jack\", Age = 20 }; var person2 = person with { }; }<\/p>\n
\u7136\u540e\u770b\u4e00\u4e0b IL \u53cd\u7f16\u8bd1\u7684\u4ee3\u7801
\n<\/p>\n
\u4e0d\u8fc7\u6211\u4e5f\u6709\u4e00\u4e2a\u7591\u95ee\uff0c\u4e3a\u5565\u8981\u9632\u7740\u6211\u76f4\u63a5\u8c03\u7528 Clone \u65b9\u6cd5\u5462?\u65b0\u4e1c\u897f\uff0c\u4e5f\u4e0d\u77e5\u9053\u5e94\u7528\u573a\u666f\uff0c\u8c01\u641e\u7684\u6e05\u695a\u54c8~~~ 😂😂😂<\/p>\n
\u56db\uff1a\u603b\u7ed3<\/strong><\/div>\n\u603b\u7684\u6765\u8bf4C#\u662f\u8d8a\u6765\u8d8a\u65b0\u9896\u4e86\uff0c\u4e5f\u4e00\u76f4\u5728\u8df5\u884c jquery \u7684\u53e3\u53f7\uff1awrite less,do more\u3002\u6709\u4e00\u70b9\u8981\u63d0\u9192\u7684\u662f\uff0c\u8bed\u6cd5\u7cd6\u591a\u4e86\uff0c\u4e00\u5b9a\u8981\u77e5\u9053\u5176\u5b9e\u5b83\u662f\u4e2a\u5565\uff0c\u4e0d\u8981\u5e38\u5e74\u6df7\u5728\u7f16\u8bd1\u5668\u4e4b\u4e0a\u8ff7\u5931\u4e86\u65b9\u5411.<\/p>\n","protected":false},"excerpt":{"rendered":"
.NET5 \u7ec8\u4e8e\u5728 2020-08-25 \u4e5f\u5c31\u662f\u524d\u5929\u53d1\u5e03\u4e86\u7b2c\u516b\u4e2a\u9884\u89c8\u7248\uff0c\u8fd9\u4e48\u591a\u7684\u9884\u89c8\u7248\u641e\u5f97\u6211\u90fd\u9ebb\u6728\u4e86\uff0c\u63a5\u8e35\u800c\u6765 […]<\/p>\n","protected":false},"author":1898,"featured_media":199918,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[593],"class_list":["post-199909","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-c9"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=199909"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199909\/revisions"}],"predecessor-version":[{"id":199931,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199909\/revisions\/199931"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/199918"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=199909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=199909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=199909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}