{"id":199909,"date":"2020-09-14T10:17:05","date_gmt":"2020-09-14T02:17:05","guid":{"rendered":"https:\/\/gulass.cn\/?p=199909"},"modified":"2020-09-01T16:17:52","modified_gmt":"2020-09-01T08:17:52","slug":"key-words-explanation","status":"publish","type":"post","link":"https:\/\/gulass.cn\/key-words-explanation.html","title":{"rendered":"C#9\u65b0\u5173\u952e\u8bcd\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n.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\u7684\u5c31\u662f\u66f4\u591a\u7684\u65b0\u7279\u6027\u52a0\u5165\u5230\u4e86 C# 9 \u4e2d\uff0c\u65e2\u7136\u8fd8\u60f3\u5446\u5728\u8fd9\u6761\u8239\u4e0a\uff0c\u5f97\u7ee7\u7eed\u786c\u7740\u5934\u76ae\u5b66\u4e60\u54c8\uff0c\u8fd9\u4e00\u7bc7\u8ddf\u5927\u5bb6\u804a\u804a\u65b0\u589e\u7684\u51e0\u4e2a\u5173\u952e\u8bcd\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\uff1a\u80cc\u666f<\/strong><\/div>\n
1. \u8bb2\u6545\u4e8b<\/strong><\/span><\/div>\n

.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\u7684\u5c31\u662f\u66f4\u591a\u7684\u65b0\u7279\u6027\u52a0\u5165\u5230\u4e86 C# 9 \u4e2d\uff0c\u65e2\u7136\u8fd8\u60f3\u5446\u5728\u8fd9\u6761\u8239\u4e0a\uff0c\u5f97\u7ee7\u7eed\u786c\u7740\u5934\u76ae\u5b66\u4e60\u54c8\uff0c\u8fd9\u4e00\u7bc7\u8ddf\u5927\u5bb6\u804a\u804a\u65b0\u589e\u7684\u51e0\u4e2a\u5173\u952e\u8bcd\u3002<\/p>\n

\u4e8c\uff1a\u65b0\u589e\u5173\u952e\u8bcd<\/strong><\/div>\n
1. init<\/strong><\/span><\/div>\n

\u51fa\u6765\u4e00\u4e2a\u65b0\u8bed\u6cd5\u7cd6\uff0c\u9996\u5148\u8981\u505a\u7684\u5c31\u662f\u53bb\u63ed\u5b83\u7684\u8001\u5e95\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u63a8\u6d4b\u5b83\u7684\u5e94\u7528\u573a\u666f\uff0c\u4e3a\u4e86\u65b9\u4fbf\u8868\u8ff0\uff0c\u6211\u5148\u4e0a\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

\r\npublic class Person \r\n    { \r\n        public string Name { get; init; } \r\n    } \r\n<\/pre>\n

\u4e4d\u4e00\u770b\u6709\u70b9\u61f5\u903c\uff0c\u6ca1\u5173\u7cfb\uff0c\u5148\u7528 ILSpy \u770b\u4e00\u4e0b\uff0c\u5982\u4e0b\u56fe\uff1a
\n\"\"
\n\u4e0a\u9762\u8fd9\u5f20\u56fe\u5c31\u5df2\u7ecf\u5f88\u6e05\u6670\u7684\u89e3\u91ca\u4e86\uff0c\u539f\u6765 init \u5c31\u662f\u81ea\u52a8\u751f\u6210\u4e86\u4e00\u4e2a\u5bf9 \u79c1\u6709\u53ea\u8bfb\u5b57\u6bb5 \u7684\u5c01\u88c5\uff0c\u5bf9\u4e8e readonly \u76f8\u4fe1\u5927\u5bb6\u5df2\u7ecf\u8f7b\u8f66\u719f\u8def\u4e86\uff0c\u5b83\u7684\u521d\u59cb\u5316\u53ea\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u58f0\u660e\u65f6\u548c\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u4f46\u4ece C# 9 \u5f00\u59cb\u5c31\u591a\u4e86\u4e00\u4e2a\u5c5e\u6027\u8d4b\u503c\u65b9\u5f0f\uff0c\u4e5f\u5c31\u662f\u8bf4\u73b0\u5728\u6709\u4e09\u79cd\u8d4b\u503c\u65b9\u5f0f\u4e86\uff0c\u8fd8\u539f\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\npublic class Person \r\n   { \r\n       private readonly string name; \r\n \r\n       public string Name \r\n       { \r\n           get => name; \r\n \r\n           init \r\n           { \r\n               name = value; \r\n           } \r\n       } \r\n   } \r\n<\/pre>\n

\u8fd9\u79cd\u65b9\u5f0f\u8981\u662f\u6362\u4f5c\u4ee5\u524d\u80af\u5b9a\u662f\u62a5\u9519\u7684\uff0c\u5982\u4e0b\u56fe:
\n\"\"
\n\u6709\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u662f\u7f16\u8bd1\u5668\u8fd8\u505a\u4e86\u4e00\u4e2a\u7279\u6b8a\u9650\u5236\uff0c\u51c6\u4f60\u5728 \u7c7b\u521d\u59cb\u5316\u5668 \u4e2d\u4f7f\u7528\uff0c\u4e0d\u51c6\u4f60\u5355\u72ec\u62ff\u51fa\u6765\u8d4b\u503c\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a
\n\"\"
\n\u6240\u4ee5\u603b\u7684\u6765\u8bf4, init \u7684\u4f5c\u7528\u5c31\u662f\u591a\u4e86\u4e00\u79cd\u8ba9\u4f60\u521d\u59cb\u5316 \u53ea\u8bfb\u5b57\u6bb5 \u7684\u65b9\u5f0f\uff0c\u4ec5\u6b64\u800c\u5df2\u7f62\u4e86\u3002<\/p>\n

2. record<\/strong><\/span><\/div>\n

\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}]}}