{"id":104648,"date":"2026-09-12T21:57:12","date_gmt":"2026-09-12T13:57:12","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/104648.html"},"modified":"2026-09-12T21:57:12","modified_gmt":"2026-09-12T13:57:12","slug":"08-03-%e4%b8%8d%e5%8f%af%e5%8f%98-immutabledictionary-tkey-tvalue-%e4%b8%8eimmutablehashset-t-%e6%8c%81%e4%b9%85%e5%8c%96%e5%93%88%e5%b8%8c%e6%a0%91","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/104648.html","title":{"rendered":"08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811"},"content":{"rendered":"<h2 id=\"immutabledictionary-\u4e0e-immutablehashset\u54c8\u5e0c\u6876\u6811-\u7ed3\u6784\u5171\u4eab\u4e0e\u539f\u5b50\u5feb\u7167\">ImmutableDictionary \u4e0e ImmutableHashSet&#xff1a;\u54c8\u5e0c\u6876\u6811\u3001\u7ed3\u6784\u5171\u4eab\u4e0e\u539f\u5b50\u5feb\u7167<\/h2>\n<p>\u7cfb\u5217&#xff1a;C# \u4e0e\u5e38\u7528\u6570\u636e\u7ed3\u6784\u6e90\u7801\u5256\u6790 \u00b7 \u4e0d\u53ef\u53d8\u96c6\u5408\u7bc7 \u9605\u8bfb\u65f6\u95f4&#xff1a;\u7ea6 60 \u5206\u949f \u524d\u7f6e\u77e5\u8bc6&#xff1a;\u54c8\u5e0c\u8868\u3001\u5e73\u8861\u6811\u3001\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784\u3001\u6bd4\u8f83\u5668 \u6e90\u7801\u57fa\u7ebf&#xff1a;System.Collections.Immutable\u00a08.0.0&#xff0c;\u5bf9\u5e94\u00a0dotnet\/runtime v8.0.0\u00a0\/ commit\u00a05535e31a712343a63f5d7d796cd874e563e5ac14&#xff1b;\u56fa\u5b9a\u6e90\u7801\u4e3a\u00a0ImmutableDictionary_2.cs\u00a0\u4e0e\u00a0ImmutableHashSet_1.cs\u3002 \u7248\u672c\u8fb9\u754c&#xff1a;\u516c\u5171\u5951\u7ea6\u4ee5\u76ee\u6807 TFM \u7684 reference assembly \u548c\u5b98\u65b9 API \u6587\u6863\u4e3a\u51c6\u3002\u672c\u6587\u7684\u5185\u90e8\u6811\u3001\u6876\u3001\u8282\u70b9\u540d\u79f0\u4e0e\u4f18\u5316\u8def\u5f84\u53ea\u6307\u4e0a\u8ff0 8.0.0\/v8.0.0\u00a0\u57fa\u7ebf&#xff0c;\u4e0d\u662f\u8de8\u7248\u672c\u627f\u8bfa&#xff1b;\u672c\u6587\u4e5f\u4e0d\u628a\u5b83\u4eec\u60f3\u5f53\u7136\u5730\u5199\u6210 HAMT\/CHAMP\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e00-\u5148\u7ea0\u6b63\u6570\u636e\u7ed3\u6784\u8fd9\u4e0d\u662f-32-\u8def-hamt\">\u4e00\u3001\u5148\u7ea0\u6b63\u6570\u636e\u7ed3\u6784&#xff1a;\u8fd9\u4e0d\u662f 32 \u8def HAMT<\/h3>\n<p>\u4e0d\u53ef\u53d8\u54c8\u5e0c\u5bb9\u5668\u7ecf\u5e38\u7528 Hash Array Mapped Trie \u5b9e\u73b0&#xff0c;\u4f46\u201c\u6301\u4e45\u5316 &#043; \u54c8\u5e0c\u201d\u4e0d\u80fd\u63a8\u5bfc\u51fa\u67d0\u4e2a .NET \u7c7b\u4e00\u5b9a\u4f7f\u7528 HAMT\u3002\u5728\u4e0a\u8ff0\u00a0System.Collections.Immutable\u00a08.0.0\/dotnet\/runtime v8.0.0\u00a0\u7684\u4e24\u4efd\u6e90\u7801\u4e2d&#xff0c;ImmutableDictionary&lt;TKey,TValue&gt;\u00a0\u548c\u00a0ImmutableHashSet&lt;T&gt;\u00a0\u53ef\u5206\u522b\u6982\u62ec\u4e3a\u5171\u4eab\u4e0b\u5217\u9ad8\u5c42\u7ed3\u6784&#xff1a;<\/p>\n<li>\u5148\u901a\u8fc7\u952e\u6216\u5143\u7d20\u7684\u76f8\u7b49\u6bd4\u8f83\u5668\u8ba1\u7b97 32 \u4f4d\u54c8\u5e0c\u7801\u3002<\/li>\n<li>\u7528\u54c8\u5e0c\u7801\u4f5c\u4e3a\u6574\u6570\u952e&#xff0c;\u5728\u5e73\u8861\u4e8c\u53c9\u6811\u4e2d\u5b9a\u4f4d\u4e00\u4e2a\u54c8\u5e0c\u6876\u3002<\/li>\n<li>\u54c8\u5e0c\u7801\u76f8\u540c\u7684\u4e0d\u540c\u952e\/\u5143\u7d20\u8fdb\u5165\u540c\u4e00\u51b2\u7a81\u6876&#xff0c;\u518d\u7528\u76f8\u7b49\u6bd4\u8f83\u5668\u533a\u5206\u3002<\/li>\n<li>\u4fee\u6539\u65f6\u91cd\u5efa\u53d7\u5f71\u54cd\u7684\u6811\u8def\u5f84\u548c\u6876&#xff0c;\u5176\u4ed6\u4e0d\u53d8\u8282\u70b9\u4e0e\u65e7\u7248\u672c\u5171\u4eab\u3002<\/li>\n<p>                         hash &#061; 25<br \/>\n                        \/         \\\\<br \/>\n                 hash &#061; 8       hash &#061; 91<br \/>\n                    |               |<br \/>\n             [hash bucket]    [hash bucket]<br \/>\n             key A -&gt; value   key X -&gt; value<br \/>\n             key B -&gt; value   \/\/ A\/B \u540c hash&#xff0c;\u4f46\u4e0d\u76f8\u7b49<\/p>\n<p>\u56e0\u6b64&#xff0c;\u539f\u6587\u4e2d\u201c\u6bcf\u6b21\u53d6 5 bit\u300132 \u8def\u5206\u652f\u3001bitmap &#043; popcount\u201d\u5e76\u4e0d\u662f\u8fd9\u4e2a\u5b9e\u73b0\u7684\u6e90\u7801\u6a21\u578b&#xff0c;\u201c\u767e\u4e07\u5143\u7d20\u53ea\u9700\u56db\u5c42\u201d\u4e5f\u4e0d\u80fd\u7528\u6765\u5206\u6790\u5b83\u3002\u672c\u6587\u4f7f\u7528\u201c\u54c8\u5e0c\u6876\u6811\u201d\u8fd9\u4e2a\u6559\u5b66\u540d\u79f0&#xff0c;\u4f46\u79c1\u6709\u7c7b\u540d\u548c\u7cbe\u786e\u5e73\u8861\u5b9e\u73b0\u4ecd\u5e94\u901a\u8fc7\u76ee\u6807 tag \u6838\u5bf9\u3002<\/p>\n<h3 id=\"\u4e8c-\u4e0d\u53ef\u53d8\u7684\u771f\u6b63\u542b\u4e49\">\u4e8c\u3001\u4e0d\u53ef\u53d8\u7684\u771f\u6b63\u542b\u4e49<\/h3>\n<p>ImmutableDictionary\u00a0\u7684\u00a0Add\u3001SetItem\u3001Remove\u00a0\u4e0d\u4f1a\u5c31\u5730\u4fee\u6539\u539f\u5bf9\u8c61&#xff0c;\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u4ee3\u8868\u65b0\u5185\u5bb9\u7684\u5b57\u5178\u3002\u65e7\u5f15\u7528\u4ecd\u7136\u6307\u5411\u65e7\u5feb\u7167&#xff1a;<\/p>\n<p>ImmutableDictionary&lt;int, string&gt; v1 &#061;<br \/>\n    ImmutableDictionary&lt;int, string&gt;.Empty;<\/p>\n<p>ImmutableDictionary&lt;int, string&gt; v2 &#061; v1.Add(1, &#034;one&#034;);<br \/>\nImmutableDictionary&lt;int, string&gt; v3 &#061; v2.SetItem(1, &#034;ONE&#034;);<\/p>\n<p>Console.WriteLine(v1.Count); \/\/ 0<br \/>\nConsole.WriteLine(v2[1]);    \/\/ one<br \/>\nConsole.WriteLine(v3[1]);    \/\/ ONE<\/p>\n<p>\u8fd9\u4e0d\u7b49\u4e8e\u6bcf\u6b21\u90fd\u590d\u5236\u5168\u90e8 n \u4e2a\u952e\u503c\u5bf9\u3002\u65b0\u7248\u672c\u53ef\u4ee5\u5171\u4eab\u672a\u53d8\u7684\u5b50\u6811\u3001\u6876\u5185\u90e8\u6301\u4e45\u5316\u8282\u70b9\u4ee5\u53ca\u6bd4\u8f83\u5668\u3002\u53ea\u6709\u4ece\u6839\u5230\u76ee\u6807\u54c8\u5e0c\u7801\u7684\u8def\u5f84\u3001\u53d1\u751f\u6539\u53d8\u7684\u51b2\u7a81\u6876\u548c\u5e73\u8861\u4fee\u590d\u6240\u9700\u8282\u70b9\u53d1\u751f\u66f4\u65b0\u3002\u8fd9\u5c31\u662f\u7ed3\u6784\u5171\u4eab\u3002<\/p>\n<p>old root                         new root<br \/>\n   \/   \\\\                           \/   \\\\<br \/>\n  A     B       Add\/Remove        A     B&#039;<br \/>\n       \/ \\\\         &#8212;-&gt;               \/ \\\\<br \/>\n      C   D                           C   D&#039;<\/p>\n<p>A \u548c C \u88ab\u65b0\u65e7\u7248\u672c\u5171\u4eab&#xff1b;\u53ea\u590d\u5236\u53d7\u5f71\u54cd\u8def\u5f84\u3002<\/p>\n<p>\u4f46\u4e0d\u53ef\u53d8\u53ea\u9002\u7528\u4e8e\u201c\u96c6\u5408\u81ea\u8eab\u7684\u952e\u503c\u5173\u7cfb\u201d\u3002\u5b83\u4e0d\u4f1a\u9012\u5f52\u51bb\u7ed3\u00a0TKey\u3001TValue\u00a0\u6216\u00a0T\u00a0\u5bf9\u8c61\u3002\u5982\u679c\u503c\u662f\u4e00\u4e2a\u53ef\u53d8\u00a0List&lt;int&gt;&#xff0c;\u4fee\u6539\u8be5\u5217\u8868\u540e&#xff0c;\u6240\u6709\u5171\u4eab\u540c\u4e00\u5217\u8868\u5f15\u7528\u7684\u5b57\u5178\u7248\u672c\u90fd\u4f1a\u89c2\u5bdf\u5230\u65b0\u5185\u5bb9\u3002<\/p>\n<p>var mutable &#061; new List&lt;int&gt; { 1 };<br \/>\nvar snapshot &#061; ImmutableDictionary&lt;string, List&lt;int&gt;&gt;.Empty<br \/>\n    .Add(&#034;numbers&#034;, mutable);<\/p>\n<p>mutable.Add(2);<br \/>\n\/\/ snapshot[&#034;numbers&#034;] \u73b0\u5728\u4e5f\u5305\u542b 2\u3002\u5b57\u5178\u4e0d\u53ef\u53d8&#xff0c;\u503c\u5e76\u4e0d\u662f\u6df1\u5c42\u4e0d\u53ef\u53d8\u3002<\/p>\n<p>\u5982\u679c\u5feb\u7167\u9700\u8981\u771f\u6b63\u9694\u79bb&#xff0c;\u503c\u4e5f\u5e94\u4f7f\u7528\u4e0d\u53ef\u53d8\u6a21\u578b\u3001\u9632\u5fa1\u6027\u526f\u672c\u6216\u6e05\u6670\u7684\u6240\u6709\u6743\u8f6c\u79fb\u3002<\/p>\n<h3 id=\"\u4e09-\u54c8\u5e0c\u6876\u6811\u7684\u4e0d\u53d8\u5f0f\">\u4e09\u3001\u54c8\u5e0c\u6876\u6811\u7684\u4e0d\u53d8\u5f0f<\/h3>\n<p>\u53ef\u4ee5\u7528\u4e09\u5c42\u4e0d\u53d8\u5f0f\u7406\u89e3\u6574\u4e2a\u5bb9\u5668\u3002\u7b2c\u4e00\u5c42\u662f\u6811&#xff1a;\u8282\u70b9\u6309\u6709\u7b26\u53f7 32 \u4f4d\u54c8\u5e0c\u7801\u7684\u786e\u5b9a\u987a\u5e8f\u7ec4\u7ec7&#xff0c;\u5de6\u5b50\u6811\u3001\u5f53\u524d\u8282\u70b9\u3001\u53f3\u5b50\u6811\u6ee1\u8db3\u641c\u7d22\u6811\u987a\u5e8f&#xff0c;\u5e76\u7ef4\u6301\u9ad8\u5ea6\u5e73\u8861\u3002<\/p>\n<p>\u7b2c\u4e8c\u5c42\u662f\u6876&#xff1a;\u6811\u4e2d\u6bcf\u4e2a\u54c8\u5e0c\u7801\u6700\u591a\u5bf9\u5e94\u4e00\u4e2a\u6876&#xff0c;\u6876\u5185\u5305\u542b\u6240\u6709\u5177\u6709\u8be5\u54c8\u5e0c\u7801\u7684\u952e\u503c\u5bf9\u6216\u96c6\u5408\u5143\u7d20\u3002\u5b83\u4eec\u53ef\u80fd\u76f8\u7b49&#xff0c;\u4e5f\u53ef\u80fd\u53ea\u662f\u54c8\u5e0c\u51b2\u7a81&#xff1b;\u9700\u8981\u518d\u7528\u76f8\u7b49\u6bd4\u8f83\u5668\u533a\u5206\u3002<\/p>\n<p>\u7b2c\u4e09\u5c42\u662f\u7248\u672c&#xff1a;\u5df2\u53d1\u5e03\u7ed9\u4e0d\u53ef\u53d8\u5bb9\u5668\u7684\u8282\u70b9\u4e0d\u80fd\u518d\u88ab\u5c31\u5730\u4fee\u6539\u3002\u65b0\u64cd\u4f5c\u53ea\u80fd\u521b\u5efa\u65b0\u8def\u5f84&#xff0c;\u5e76\u8ba9\u65b0\u6839\u6307\u5411\u65e7\u5b50\u6811\u548c\u65b0\u8282\u70b9\u7684\u7ec4\u5408\u3002Builder \u53ef\u4ee5\u5728\u72ec\u5360\u7684\u53ef\u53d8\u9636\u6bb5\u91cc\u590d\u7528\u8282\u70b9&#xff0c;\u4f46\u53d1\u5e03\u4e3a\u4e0d\u53ef\u53d8\u5feb\u7167\u540e\u5fc5\u987b\u9075\u5b88\u8fd9\u4e2a\u8fb9\u754c\u3002<\/p>\n<h3 id=\"\u56db-\u6bd4\u8f83\u5668\u662f\u5bb9\u5668\u8bed\u4e49\u7684\u4e00\u90e8\u5206\">\u56db\u3001\u6bd4\u8f83\u5668\u662f\u5bb9\u5668\u8bed\u4e49\u7684\u4e00\u90e8\u5206<\/h3>\n<p>ImmutableDictionary&lt;TKey,TValue&gt;\u00a0\u81f3\u5c11\u6d89\u53ca\u952e\u6bd4\u8f83\u5668\u548c\u503c\u6bd4\u8f83\u5668\u3002\u952e\u6bd4\u8f83\u5668\u51b3\u5b9a\u54c8\u5e0c\u7801\u548c\u952e\u76f8\u7b49&#xff0c;\u56e0\u6b64\u51b3\u5b9a\u5b57\u5178\u4e2d\u201c\u540c\u4e00\u4e2a\u952e\u201d\u7684\u610f\u4e49\u3002\u503c\u6bd4\u8f83\u5668\u7528\u4e8e\u9700\u8981\u5224\u65ad\u5df2\u6709\u503c\u4e0e\u65b0\u503c\u662f\u5426\u76f8\u540c\u7684\u64cd\u4f5c\u8bed\u4e49\u3002\u5e94\u5728\u521b\u5efa\u7a7a\u96c6\u5408\u65f6\u5c31\u9009\u5b9a\u6bd4\u8f83\u5668&#xff1a;<\/p>\n<p>ImmutableDictionary&lt;string, int&gt; scores &#061;<br \/>\n    ImmutableDictionary.Create&lt;string, int&gt;(StringComparer.OrdinalIgnoreCase);<\/p>\n<p>scores &#061; scores.Add(&#034;Player&#034;, 10);<br \/>\n\/\/ scores.Add(&#034;player&#034;, 20) \u4f1a\u9047\u5230\u5df2\u6709\u952e\u3002<\/p>\n<p>\u4e00\u4e2a\u6b63\u786e\u7684\u76f8\u7b49\u6bd4\u8f83\u5668\u5fc5\u987b\u6ee1\u8db3&#xff1a;\u82e5\u00a0Equals(a,b)\u00a0\u4e3a\u771f&#xff0c;\u5219\u00a0GetHashCode(a)\u00a0\u5fc5\u987b\u7b49\u4e8e\u00a0GetHashCode(b)\u3002\u54c8\u5e0c\u76f8\u540c\u4e0d\u5fc5\u63a8\u51fa\u76f8\u7b49&#xff0c;\u51b2\u7a81\u6876\u5c31\u662f\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002\u6bd4\u8f83\u7ed3\u679c\u8fd8\u5fc5\u987b\u5728\u952e\u5b58\u6d3b\u671f\u95f4\u7a33\u5b9a&#xff1b;\u4e0d\u80fd\u4f9d\u8d56\u4f1a\u6539\u53d8\u7684\u5168\u5c40\u72b6\u6001\u3002<\/p>\n<p>WithComparers\u00a0\u4e00\u7c7b API \u8868\u9762\u4e0a\u53ea\u662f\u6362\u6bd4\u8f83\u5668&#xff0c;\u8bed\u4e49\u4e0a\u5374\u53ef\u80fd\u6539\u53d8\u952e\u7b49\u4ef7\u7c7b\u3002\u4f8b\u5982\u4ece ordinal \u6362\u5230 ordinal-ignore-case \u65f6&#xff0c;\u539f\u6765\u4e24\u4e2a\u4e0d\u540c\u952e\u53ef\u80fd\u5408\u5e76\u4e3a\u51b2\u7a81\u3002\u5177\u4f53 API \u5bf9\u8fd9\u79cd\u51b2\u7a81\u5982\u4f55\u5904\u7406\u5e94\u67e5\u76ee\u6807\u7248\u672c\u5951\u7ea6&#xff0c;\u4e0d\u80fd\u628a\u66f4\u6362\u6bd4\u8f83\u5668\u5f53\u6210 O(1) \u5b57\u6bb5\u8d4b\u503c\u3002<\/p>\n<h3 id=\"\u4e94-add-setitem-\u548c-remove\">\u4e94\u3001Add\u3001SetItem \u548c Remove<\/h3>\n<p>Add(key,value)\u00a0\u8868\u793a\u201c\u6dfb\u52a0\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u952e\u201d\u3002\u5df2\u5b58\u5728\u7b49\u4ef7\u952e\u65f6&#xff0c;\u82e5\u4e0d\u6ee1\u8db3\u76ee\u6807 API \u5141\u8bb8\u7684\u5e42\u7b49\u60c5\u51b5&#xff0c;\u5b83\u4f1a\u901a\u8fc7\u5f02\u5e38\u62a5\u544a\u51b2\u7a81\u3002\u8fd9\u9002\u5408\u91cd\u590d\u5c31\u662f\u9519\u8bef\u7684\u6ce8\u518c\u8fb9\u754c\u3002<\/p>\n<p>SetItem(key,value)\u00a0\u8868\u793a upsert&#xff1a;\u952e\u4e0d\u5b58\u5728\u65f6\u6dfb\u52a0&#xff0c;\u952e\u5b58\u5728\u65f6\u66ff\u6362\u503c\u3002\u5982\u679c\u64cd\u4f5c\u6ca1\u6709\u9020\u6210\u903b\u8f91\u53d8\u5316&#xff0c;\u5b9e\u73b0\u53ef\u80fd\u8fd4\u56de\u539f\u5b9e\u4f8b&#xff0c;\u4f46\u8c03\u7528\u8005\u4e0d\u5e94\u7528\u5f15\u7528\u76f8\u7b49\u4ee3\u66ff\u96c6\u5408\u5185\u5bb9\u8bed\u4e49\u3002<\/p>\n<p>Remove(key)\u00a0\u5728\u952e\u5b58\u5728\u65f6\u4ece\u51b2\u7a81\u6876\u4e2d\u79fb\u9664\u8be5\u952e\u503c\u5bf9\u3002\u6876\u4e2d\u8fd8\u6709\u5176\u4ed6\u51b2\u7a81\u952e\u65f6&#xff0c;\u66ff\u6362\u8be5\u6876&#xff1b;\u6876\u53d8\u7a7a\u65f6&#xff0c;\u4ece\u54c8\u5e0c\u6811\u4e2d\u79fb\u9664\u6574\u4e2a\u54c8\u5e0c\u7801\u8282\u70b9\u5e76\u4fee\u590d\u5e73\u8861\u3002\u952e\u4e0d\u5b58\u5728\u65f6&#xff0c;\u7ed3\u679c\u5728\u5185\u5bb9\u4e0a\u4e0e\u539f\u5b57\u5178\u4e00\u81f4\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u7ed3\u6784\u5316\u4f2a\u4ee3\u7801&#xff0c;\u4e0d\u662f\u4efb\u4f55 runtime tag \u7684\u9010\u5b57\u6e90\u7801&#xff1a;<\/p>\n<p>\/\/ \u4f2a\u4ee3\u7801&#xff1a;\u7528\u4e8e\u89e3\u91ca\u8def\u5f84\u590d\u5236\u548c\u6876\u66f4\u65b0\u3002<br \/>\nNode SetItem(Node node, int hash, TKey key, TValue value)<br \/>\n{<br \/>\n    if (node.IsEmpty)<br \/>\n        return NewNode(hash, Bucket.Single(key, value));<\/p>\n<p>    if (hash &lt; node.Hash)<br \/>\n        return Balance(node.WithLeft(SetItem(node.Left, hash, key, value)));<\/p>\n<p>    if (hash &gt; node.Hash)<br \/>\n        return Balance(node.WithRight(SetItem(node.Right, hash, key, value)));<\/p>\n<p>    Bucket updated &#061; node.Bucket.SetItem(key, value, _keyComparer);<br \/>\n    return node.WithBucket(updated);<br \/>\n}<\/p>\n<p>WithLeft\u3001WithRight\u00a0\u548c\u00a0WithBucket\u00a0\u5728\u4e0d\u53ef\u53d8\u8def\u5f84\u4e0a\u4ea7\u751f\u65b0\u8282\u70b9&#xff0c;\u672a\u8d70\u8fc7\u7684\u5b50\u6811\u5f15\u7528\u88ab\u539f\u6837\u590d\u7528\u3002Balance\u00a0\u53ea\u5bf9\u88ab\u4fee\u6539\u8def\u5f84\u505a\u5c40\u90e8\u65cb\u8f6c\u3002\u771f\u5b9e\u6e90\u7801\u8fd8\u5305\u542b\u53d8\u66f4\u7ed3\u679c\u3001\u8ba1\u6570\u3001\u5df2\u51bb\u7ed3\u8282\u70b9\u4e0e\u5404\u79cd\u5feb\u8def\u5f84&#xff0c;\u9700\u6309\u56fa\u5b9a tag \u9605\u8bfb\u3002<\/p>\n<h3 id=\"\u516d-immutablehashset-\u7684\u540c\u4e0e\u4e0d\u540c\">\u516d\u3001ImmutableHashSet \u7684\u540c\u4e0e\u4e0d\u540c<\/h3>\n<p>ImmutableHashSet&lt;T&gt;\u00a0\u53ea\u4fdd\u5b58\u552f\u4e00\u5143\u7d20&#xff0c;\u6ca1\u6709\u72ec\u7acb TValue\u3002\u5728\u76ee\u6807\u5b9e\u73b0\u4e2d&#xff0c;\u5b83\u4e0e\u4e0d\u53ef\u53d8\u5b57\u5178\u5171\u4eab\u201c\u54c8\u5e0c\u7801\u5e73\u8861\u6811 &#043; \u51b2\u7a81\u6876\u201d\u7684\u6574\u4f53\u601d\u8def&#xff0c;\u4f46\u6876\u7684\u5143\u7d20\u548c\u66f4\u65b0\u8bed\u4e49\u4e0d\u540c\u3002\u5b83\u4f7f\u7528\u00a0IEqualityComparer&lt;T&gt;\u00a0\u540c\u65f6\u5b9a\u4e49\u5143\u7d20\u7684\u54c8\u5e0c\u548c\u76f8\u7b49\u3002<\/p>\n<p>ImmutableHashSet&lt;string&gt; tags &#061;<br \/>\n    ImmutableHashSet.Create&lt;string&gt;(StringComparer.OrdinalIgnoreCase);<\/p>\n<p>tags &#061; tags.Add(&#034;Boss&#034;);<br \/>\ntags &#061; tags.Add(&#034;boss&#034;); \/\/ \u903b\u8f91\u5185\u5bb9\u4ecd\u53ea\u6709\u4e00\u4e2a\u7b49\u4ef7\u5143\u7d20<\/p>\n<p>\u96c6\u5408\u7684\u5e76\u96c6\u3001\u4ea4\u96c6\u3001\u5dee\u96c6\u548c\u5bf9\u79f0\u5dee\u96c6\u90fd\u4f1a\u8fd4\u56de\u65b0\u96c6\u5408&#xff0c;\u4f46\u65b0\u5b9e\u4f8b\u53ef\u4ee5\u5171\u4eab\u672a\u53d8\u7ed3\u6784\u3002\u5177\u4f53\u7b97\u6cd5\u662f\u5426\u4f1a\u5229\u7528\u53e6\u4e00\u4e2a\u540c\u7c7b\u96c6\u5408\u7684\u5185\u90e8\u7ed3\u6784\u3001\u54ea\u4e2a\u64cd\u4f5c\u4f1a\u8f6c\u5230 Builder&#xff0c;\u90fd\u5c5e\u4e8e\u9700\u6309\u7248\u672c\u6838\u9a8c\u7684\u4f18\u5316\u3002<\/p>\n<h3 id=\"\u4e03-builder\u5728\u53ef\u53d8\u9636\u6bb5\u6279\u91cf\u4fee\u6539\">\u4e03\u3001Builder&#xff1a;\u5728\u53ef\u53d8\u9636\u6bb5\u6279\u91cf\u4fee\u6539<\/h3>\n<p>\u8fde\u7eed\u5199\u00a0map &#061; map.SetItem(&#8230;)\u00a0\u5728\u8bed\u4e49\u4e0a\u5b8c\u5168\u6b63\u786e&#xff0c;\u4f46\u6bcf\u6b65\u90fd\u8981\u4ea7\u751f\u4e00\u4e2a\u53ef\u72ec\u7acb\u53d1\u5e03\u7684\u4e0d\u53ef\u53d8\u6839\u3002\u5f53\u4e1a\u52a1\u662f\u201c\u5728\u5355\u4e00\u6240\u6709\u8005\u4e2d\u6267\u884c\u4e00\u6279\u4fee\u6539&#xff0c;\u7136\u540e\u53d1\u5e03\u4e00\u4e2a\u5feb\u7167\u201d\u65f6&#xff0c;Builder \u53ef\u4ee5\u51cf\u5c11\u4e2d\u95f4\u7248\u672c\u548c\u90e8\u5206\u8def\u5f84\u5206\u914d\u3002<\/p>\n<p>ImmutableDictionary&lt;string, Player&gt;.Builder builder &#061; snapshot.ToBuilder();<\/p>\n<p>foreach (PlayerDelta delta in deltas)<br \/>\n{<br \/>\n    if (delta.IsRemoved)<br \/>\n        builder.Remove(delta.Id);<br \/>\n    else<br \/>\n        builder[delta.Id] &#061; delta.Player;<br \/>\n}<\/p>\n<p>ImmutableDictionary&lt;string, Player&gt; next &#061; builder.ToImmutable();<\/p>\n<p>Builder \u662f\u53ef\u53d8\u5bf9\u8c61&#xff0c;\u4e0d\u7ee7\u627f\u4e0d\u53ef\u53d8\u5feb\u7167\u7684\u65e0\u9501\u5e76\u53d1\u8bfb\u7279\u6027\u3002\u4e0d\u5e94\u8ba9\u591a\u4e2a\u7ebf\u7a0b\u65e0\u540c\u6b65\u5171\u4eab\u4e00\u4e2a Builder\u3002\u4e00\u4e2a\u5b89\u5168\u6a21\u5f0f\u662f&#xff1a;\u66f4\u65b0\u7ebf\u7a0b\u72ec\u5360 Builder&#xff0c;\u8bfb\u8005\u53ea\u80fd\u770b\u5230\u4e0a\u4e00\u4e2a\u5df2\u53d1\u5e03\u7684\u00a0ImmutableDictionary&#xff0c;\u6574\u6279\u66f4\u65b0\u5b8c\u6210\u540e\u624d\u66ff\u6362\u5feb\u7167\u5f15\u7528\u3002<\/p>\n<p>ToImmutable()\u00a0\u4e4b\u540e Builder \u4ecd\u53ef\u4ee5\u7ee7\u7eed\u4fee\u6539&#xff0c;\u4f46\u5df2\u8fd4\u56de\u7684\u5feb\u7167\u4e0d\u4f1a\u88ab\u540e\u7eed\u4fee\u6539\u6c61\u67d3\u3002\u8fd9\u9700\u8981\u5b9e\u73b0\u5728\u8282\u70b9\u4e0a\u7ef4\u62a4\u53ef\u53d8\/\u51bb\u7ed3\u6240\u6709\u6743\u8fb9\u754c&#xff0c;\u5e76\u5728\u5fc5\u8981\u65f6\u6062\u590d\u8def\u5f84\u590d\u5236&#xff1b;\u4e0d\u80fd\u628a Builder \u7406\u89e3\u6210\u76f4\u63a5\u7be1\u6539\u5df2\u53d1\u5e03\u6811\u3002<\/p>\n<h3 id=\"\u516b-\u679a\u4e3e\u987a\u5e8f\u4e0d\u662f\u54c8\u5e0c\u5bb9\u5668\u7684\u6301\u4e45\u5316\u5951\u7ea6\">\u516b\u3001\u679a\u4e3e\u987a\u5e8f\u4e0d\u662f\u54c8\u5e0c\u5bb9\u5668\u7684\u6301\u4e45\u5316\u5951\u7ea6<\/h3>\n<p>\u4e0d\u53ef\u53d8\u96c6\u5408\u5728\u679a\u4e3e\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u88ab\u4fee\u6539&#xff0c;\u56e0\u6b64\u679a\u4e3e\u5668\u4e0d\u9700\u8981\u50cf\u53ef\u53d8\u00a0Dictionary\u00a0\u4e00\u6837\u5bf9\u540e\u7eed\u5199\u505a fail-fast \u7248\u672c\u68c0\u6d4b\u3002\u8fd9\u975e\u5e38\u9002\u5408\u957f\u65f6\u95f4\u8bfb\u5feb\u7167&#xff1a;\u5199\u8005\u521b\u5efa\u65b0\u6839&#xff0c;\u5df2\u7ecf\u83b7\u5f97\u65e7\u6839\u7684\u679a\u4e3e\u5668\u7ee7\u7eed\u8bfb\u65e7\u7ed3\u6784\u3002<\/p>\n<p>\u4f46\u679a\u4e3e\u987a\u5e8f\u7531\u5185\u90e8\u54c8\u5e0c\u7801\u6811\u3001\u54c8\u5e0c\u6bd4\u8f83\u5668\u548c\u51b2\u7a81\u6876\u5e03\u5c40\u51b3\u5b9a&#xff0c;\u4e0d\u662f\u63d2\u5165\u987a\u5e8f\u3001\u952e\u6392\u5e8f\u6216\u8de8\u8fd0\u884c\u65f6\u7a33\u5b9a\u987a\u5e8f\u7684\u627f\u8bfa\u3002\u5b57\u7b26\u4e32\u54c8\u5e0c\u7b56\u7565\u3001\u6bd4\u8f83\u5668\u6216\u79c1\u6709\u5b9e\u73b0\u6539\u53d8\u65f6&#xff0c;\u987a\u5e8f\u53ef\u4ee5\u6539\u53d8\u3002<\/p>\n<p>\u5982\u679c\u9700\u8981\u786e\u5b9a\u6027\u5e8f\u5217\u5316\u3001\u8de8\u8bbe\u5907\u54c8\u5e0c\u6216\u53ef\u8bfb diff&#xff0c;\u5e94\u5728\u8f93\u51fa\u8fb9\u754c\u6309\u660e\u786e\u6bd4\u8f83\u89c4\u5219\u6392\u5e8f&#xff0c;\u4e0d\u80fd\u4f9d\u8d56\u5f53\u524d\u679a\u4e3e\u7684\u5076\u7136\u987a\u5e8f\u3002\u6392\u5e8f\u4f1a\u4ea7\u751f\u81ea\u5df1\u7684\u65f6\u95f4\u548c\u5feb\u7167\u5185\u5b58\u6210\u672c&#xff0c;\u5e94\u53ea\u5728\u771f\u6b63\u9700\u8981\u89c4\u8303\u5316\u7684\u8fb9\u754c\u505a\u3002<\/p>\n<h3 id=\"\u4e5d-\u590d\u6742\u5ea6\u5e73\u8861\u6811\u9ad8\u5ea6\u4e0e\u51b2\u7a81\u6876\u540c\u65f6\u5b58\u5728\">\u4e5d\u3001\u590d\u6742\u5ea6&#xff1a;\u5e73\u8861\u6811\u9ad8\u5ea6\u4e0e\u51b2\u7a81\u6876\u540c\u65f6\u5b58\u5728<\/h3>\n<p>\u8bbe\u4e0d\u540c\u54c8\u5e0c\u7801\u7684\u6570\u91cf\u4e3a\u00a0h&#xff0c;\u76ee\u6807\u54c8\u5e0c\u7801\u6876\u5185\u6709\u00a0c\u00a0\u4e2a\u51b2\u7a81\u5143\u7d20\u3002\u5728\u76ee\u6807\u5b9e\u73b0\u6a21\u578b\u4e0b&#xff0c;\u67e5\u627e\u3001\u6dfb\u52a0\u6216\u5220\u9664\u9996\u5148\u652f\u4ed8 O(log h) \u7684\u5e73\u8861\u6811\u5b9a\u4f4d&#xff0c;\u518d\u652f\u4ed8\u51b2\u7a81\u6876\u4e2d\u76f8\u7b49\u6bd4\u8f83\u7684\u6210\u672c\u3002\u5f53\u54c8\u5e0c\u5206\u5e03\u826f\u597d\u65f6&#xff0c;c\u00a0\u5f88\u5c0f&#xff1b;\u5f53\u5927\u91cf\u952e\u8fd4\u56de\u540c\u4e00\u54c8\u5e0c\u7801\u65f6&#xff0c;\u64cd\u4f5c\u53ef\u9000\u5316\u4e3a\u5bf9\u957f\u51b2\u7a81\u6876\u7684\u7ebf\u6027\u641c\u7d22\u3002<\/p>\n<table>\n<tr>\u64cd\u4f5c\u826f\u597d\u54c8\u5e0c\u5206\u5e03\u7684\u6a21\u578b\u6781\u7aef\u51b2\u7a81\u7684\u8fb9\u754c<\/tr>\n<tbody>\n<tr>\n<td>\u6309\u952e\/\u5143\u7d20\u67e5\u627e<\/td>\n<td>O(log h &#043; c)<\/td>\n<td>\u53ef\u9000\u5316\u81f3 O(n)<\/td>\n<\/tr>\n<tr>\n<td>Add \/ SetItem \/ Remove<\/td>\n<td>\u6811\u8def\u5f84 &#043; \u6876\u66f4\u65b0<\/td>\n<td>\u6876\u641c\u7d22\/\u66f4\u65b0\u53ef O(n)<\/td>\n<\/tr>\n<tr>\n<td>\u679a\u4e3e<\/td>\n<td>O(n)<\/td>\n<td>O(n)<\/td>\n<\/tr>\n<tr>\n<td>Builder \u6279\u91cf\u66f4\u65b0<\/td>\n<td>\u51cf\u5c11\u4e2d\u95f4\u5206\u914d<\/td>\n<td>\u4e0d\u4f1a\u4fee\u590d\u574f\u54c8\u5e0c\u51fd\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8868\u4e2d\u6ca1\u6709\u5199\u201cO(1) \u54c8\u5e0c\u67e5\u627e\u201d&#xff0c;\u56e0\u4e3a\u8be5\u6e90\u7801\u6a21\u578b\u7684\u4e00\u7ea7\u7d22\u5f15\u662f\u54c8\u5e0c\u7801\u5e73\u8861\u6811&#xff0c;\u4e0d\u662f\u53ef\u76f4\u63a5\u6309\u6876\u4f59\u6570\u5b9a\u4f4d\u7684\u53ef\u53d8\u6570\u7ec4\u3002\u4e5f\u4e0d\u80fd\u5199\u6210\u00a0O(log_32 n)&#xff0c;\u56e0\u4e3a\u8fd9\u4e0d\u662f 32 \u8def trie\u3002<\/p>\n<p>\u6e10\u8fd1\u590d\u6742\u5ea6\u4e0d\u8bf4\u660e\u5206\u914d\u3001\u8282\u70b9\u8df3\u8f6c\u548c\u6bd4\u8f83\u5668\u6210\u672c\u3002\u4e0d\u53ef\u53d8\u6811\u4e3a\u5355\u6b21\u66f4\u65b0\u5206\u914d\u65b0\u8def\u5f84&#xff0c;\u5c40\u90e8\u6027\u901a\u5e38\u4e0d\u5982\u8fde\u7eed\u00a0Dictionary\u00a0Entry \u6570\u7ec4\u3002\u7ed3\u6784\u5171\u4eab\u8282\u7701\u4e86\u6574\u4f53\u590d\u5236&#xff0c;\u5374\u4e0d\u4ee3\u8868\u201c\u65e0\u5206\u914d\u201d\u6216\u201c\u4e0e\u53ef\u53d8\u5b57\u5178\u901f\u5ea6\u76f8\u540c\u201d\u3002\u5fc5\u987b\u6839\u636e\u7248\u672c\u6570\u91cf\u3001\u66f4\u65b0\u6279\u6b21\u3001\u952e\u5206\u5e03\u548c\u76ee\u6807 runtime \u5b9e\u6d4b\u3002<\/p>\n<h3 id=\"\u5341-\u952e\u4e0d\u53d8\u5f0f\u4e0d\u53ef\u53d8\u5b57\u5178\u4e5f\u6015\u53ef\u53d8\u952e\">\u5341\u3001\u952e\u4e0d\u53d8\u5f0f&#xff1a;\u4e0d\u53ef\u53d8\u5b57\u5178\u4e5f\u6015\u53ef\u53d8\u952e<\/h3>\n<p>\u5b57\u5178\u4e0d\u4fee\u6539\u81ea\u5df1&#xff0c;\u4e0d\u610f\u5473\u5b83\u80fd\u9632\u6b62\u5916\u90e8\u4fee\u6539\u952e\u5bf9\u8c61\u3002\u952e\u52a0\u5165\u540e&#xff0c;\u4efb\u4f55\u5f71\u54cd\u00a0GetHashCode\u00a0\u6216\u00a0Equals\u00a0\u7684\u72b6\u6001\u90fd\u5fc5\u987b\u4fdd\u6301\u4e0d\u53d8\u3002\u5426\u5219\u952e\u4ecd\u7136\u5b58\u50a8\u5728\u65e7\u54c8\u5e0c\u7801\u5bf9\u5e94\u7684\u6811\u8282\u70b9\u4e2d&#xff0c;\u4f46\u67e5\u627e\u4f1a\u8ba1\u7b97\u65b0\u54c8\u5e0c\u7801\u5e76\u8d70\u5411\u53e6\u4e00\u4e2a\u8282\u70b9\u3002<\/p>\n<p>public sealed class BadKey<br \/>\n{<br \/>\n    public string Name { get; set; } &#061; &#034;&#034;;<br \/>\n    public override int GetHashCode() &#061;&gt; Name.GetHashCode();<br \/>\n    public override bool Equals(object? obj) &#061;&gt;<br \/>\n        obj is BadKey other &amp;&amp; Name &#061;&#061; other.Name;<br \/>\n}<\/p>\n<p>\u8fd9\u79cd\u952e\u5373\u4f7f\u653e\u5165\u00a0ImmutableDictionary\u4e5f\u4e0d\u5b89\u5168\u3002\u66f4\u597d\u7684\u952e\u662f\u4e0d\u53ef\u53d8\u503c\u5bf9\u8c61\u3001\u5c01\u88c5\u7684\u6574\u6570 ID \u6216\u4e0d\u53ef\u53d8\u5b57\u7b26\u4e32\u3002\u82e5\u4e1a\u52a1\u5b9e\u4f53\u7684\u540d\u79f0\u4f1a\u53d8&#xff0c;\u4f7f\u7528\u7a33\u5b9a ID \u4f5c\u952e&#xff0c;\u5c06\u540d\u79f0\u653e\u5728\u503c\u4e2d\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u53cd\u4f8b\u662f\u6240\u6709\u952e\u90fd\u8fd4\u56de\u5e38\u91cf\u54c8\u5e0c\u7801\u3002\u5b83\u53ef\u4ee5\u6ee1\u8db3\u201c\u76f8\u7b49\u952e\u54c8\u5e0c\u76f8\u540c\u201d\u7684\u6700\u4f4e\u6b63\u786e\u6027\u5951\u7ea6&#xff0c;\u5374\u4f1a\u628a\u5168\u90e8\u5143\u7d20\u653e\u5165\u4e00\u4e2a\u51b2\u7a81\u6876&#xff0c;\u4f7f\u67e5\u627e\u4e0e\u66f4\u65b0\u9000\u5316\u3002\u6b63\u786e\u7684\u54c8\u5e0c\u51fd\u6570\u8fd8\u5e94\u5c3d\u91cf\u4f7f\u5178\u578b\u952e\u5747\u5300\u5206\u5e03&#xff0c;\u4f46\u4e0d\u8981\u4e3a\u4e86\u5747\u5300\u800c\u7834\u574f\u7b49\u4ef7\u5951\u7ea6\u3002<\/p>\n<h3 id=\"\u5341\u4e00-\u539f\u5b50\u53d1\u5e03\u4e0e\u591a\u6b65\u66f4\u65b0\">\u5341\u4e00\u3001\u539f\u5b50\u53d1\u5e03\u4e0e\u591a\u6b65\u66f4\u65b0<\/h3>\n<p>\u4e0d\u53ef\u53d8\u5bf9\u8c61\u975e\u5e38\u9002\u5408\u5feb\u7167\u53d1\u5e03&#xff1a;\u5199\u8005\u5728\u79c1\u6709\u5c40\u90e8\u53d8\u91cf\u4e2d\u6784\u5efa\u5b8c\u6574\u65b0\u5b57\u5178&#xff0c;\u7136\u540e\u4e00\u6b21\u66ff\u6362\u5171\u4eab\u6839\u5f15\u7528\u3002\u8bfb\u8005\u53ea\u9700\u8bfb\u53d6\u4e00\u6b21\u6839\u5f15\u7528&#xff0c;\u6574\u4e2a\u64cd\u4f5c\u671f\u95f4\u90fd\u4f7f\u7528\u540c\u4e00\u5feb\u7167\u3002<\/p>\n<p>private ImmutableDictionary&lt;string, Player&gt; _players &#061;<br \/>\n    ImmutableDictionary&lt;string, Player&gt;.Empty;<\/p>\n<p>public ImmutableDictionary&lt;string, Player&gt; Snapshot() &#061;&gt;<br \/>\n    Volatile.Read(ref _players);<\/p>\n<p>public void Publish(ImmutableDictionary&lt;string, Player&gt; next) &#061;&gt;<br \/>\n    Volatile.Write(ref _players, next);<\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u53ea\u9002\u5408\u5355\u5199\u8005&#xff0c;\u6216\u5199\u8005\u5df2\u7531\u5916\u90e8\u4e32\u884c\u5316\u7684\u7cfb\u7edf\u3002\u82e5\u4e24\u4e2a\u5199\u8005\u90fd\u8bfb\u53d6\u540c\u4e00\u65e7\u5feb\u7167&#xff0c;\u5404\u81ea\u6dfb\u52a0\u4e0d\u540c\u952e\u5e76\u5148\u540e\u00a0Volatile.Write&#xff0c;\u540e\u53d1\u5e03\u8005\u4f1a\u8986\u76d6\u524d\u8005\u7684\u66f4\u65b0\u3002\u4e0d\u53ef\u53d8\u4fdd\u8bc1\u5feb\u7167\u4e0d\u88ab\u5c31\u5730\u7834\u574f&#xff0c;\u4e0d\u4fdd\u8bc1 read-modify-write \u7ec4\u5408\u81ea\u52a8\u539f\u5b50\u3002<\/p>\n<p>\u591a\u5199\u8005\u53ef\u4ee5\u7528 CAS \u5faa\u73af&#xff0c;\u4ece\u5f53\u524d\u5feb\u7167\u8ba1\u7b97\u65b0\u5feb\u7167&#xff0c;\u53ea\u6709\u5f53\u6839\u4ecd\u662f\u539f\u6765\u90a3\u4e2a\u5f15\u7528\u65f6\u624d\u66ff\u6362&#xff0c;\u5931\u8d25\u5219\u57fa\u4e8e\u65b0\u6839\u91cd\u7b97\u3002\u6807\u51c6\u4e0d\u53ef\u53d8\u5e2e\u52a9 API \u63d0\u4f9b\u4e86\u5bf9\u5e94\u7684\u539f\u5b50\u66f4\u65b0\u6a21\u5f0f&#xff0c;\u4f46\u5177\u4f53\u91cd\u8bd5\u56de\u8c03\u53ef\u80fd\u6267\u884c\u591a\u6b21&#xff0c;\u4e0d\u80fd\u5728\u5176\u4e2d\u76f4\u63a5\u53d1\u9001\u90ae\u4ef6\u3001\u6263\u6b3e\u6216\u4ea7\u751f\u4e0d\u53ef\u91cd\u590d\u7684\u526f\u4f5c\u7528\u3002<\/p>\n<p>\/\/ \u7ed3\u6784\u5316\u6a21\u5f0f&#xff1a;\u5b9e\u9645\u9879\u76ee\u4f18\u5148\u4f7f\u7528\u76ee\u6807\u7248\u672c\u7684 ImmutableInterlocked API\u3002<br \/>\nwhile (true)<br \/>\n{<br \/>\n    var before &#061; Volatile.Read(ref _players);<br \/>\n    var after &#061; before.SetItem(player.Id, player);<\/p>\n<p>    if (ReferenceEquals(<br \/>\n        Interlocked.CompareExchange(ref _players, after, before),<br \/>\n        before))<br \/>\n        break;<br \/>\n}<\/p>\n<p>\u591a\u4e2a\u952e\u9700\u8981\u5171\u540c\u6ee1\u8db3\u4e1a\u52a1\u4e0d\u53d8\u5f0f\u65f6&#xff0c;\u5e94\u5728\u4e00\u4e2a\u5c40\u90e8\u5feb\u7167\u4e0a\u5b8c\u6210\u6240\u6709\u00a0SetItem\/Remove&#xff0c;\u6700\u540e\u53ea\u53d1\u5e03\u4e00\u6b21\u6839\u3002\u4e0d\u8981\u6bcf\u6539\u4e00\u4e2a\u952e\u5c31\u53d1\u5e03&#xff0c;\u5426\u5219\u8bfb\u8005\u53ef\u80fd\u770b\u5230\u4e1a\u52a1\u4e0a\u65e0\u6548\u7684\u4e2d\u95f4\u72b6\u6001\u3002<\/p>\n<h3 id=\"\u5341\u4e8c-gc-\u6210\u672c\u4e0e\u7248\u672c\u4fdd\u7559\">\u5341\u4e8c\u3001GC \u6210\u672c\u4e0e\u7248\u672c\u4fdd\u7559<\/h3>\n<p>\u7ed3\u6784\u5171\u4eab\u964d\u4f4e\u4e86\u201c\u6bcf\u6b21\u5feb\u7167\u590d\u5236\u6574\u4e2a\u5b57\u5178\u201d\u7684\u6210\u672c&#xff0c;\u4f46\u65b0\u8def\u5f84\u3001\u65b0\u6876\u548c\u65b0\u6839\u4ecd\u662f\u6258\u7ba1\u5bf9\u8c61\u3002\u9ad8\u9891\u5355\u9879\u66f4\u65b0\u53ef\u4ee5\u4ea7\u751f\u5927\u91cf\u77ed\u547d\u8282\u70b9&#xff1b;Builder \u80fd\u964d\u4f4e\u6279\u5904\u7406\u4e2d\u95f4\u5206\u914d&#xff0c;\u5374\u4e0d\u4f1a\u628a\u6700\u7ec8\u4e0d\u53ef\u53d8\u7ed3\u6784\u53d8\u6210\u8fde\u7eed\u6570\u7ec4\u3002<\/p>\n<p>\u53ea\u8981\u67d0\u4e2a\u65e7\u5feb\u7167\u4ecd\u88ab\u5f15\u7528&#xff0c;\u5b83\u72ec\u6709\u7684\u8282\u70b9\u4ee5\u53ca\u901a\u8fc7\u5171\u4eab\u8282\u70b9\u53ef\u8fbe\u7684\u952e\u503c\u5bf9\u5c31\u4e0d\u80fd\u88ab GC \u56de\u6536\u3002\u8fd9\u662f\u7248\u672c\u5feb\u7167\u6b63\u786e\u6027\u7684\u5fc5\u7136\u7ed3\u679c\u3002\u65e0\u754c\u4fdd\u7559\u6bcf\u4e00\u4e2a\u5386\u53f2\u7248\u672c&#xff0c;\u4f1a\u4f7f\u5185\u5b58\u968f\u4e1a\u52a1\u5386\u53f2\u589e\u957f&#xff0c;\u5e76\u4e0d\u662f\u201c\u56e0\u4e3a\u5171\u4eab\u5c31\u51e0\u4e4e\u514d\u8d39\u201d\u3002<\/p>\n<p>\u9700\u8981 undo\/redo \u65f6\u5e94\u8bbe\u5b9a\u7248\u672c\u4e0a\u9650\u3001\u68c0\u67e5\u5927\u5bf9\u8c61\u503c\u548c\u6df1\u5c42\u53ef\u53d8\u503c&#xff0c;\u5e76\u7528\u5806\u5feb\u7167\u5b9a\u4f4d\u4fdd\u7559\u6839\u3002\u9700\u8981\u957f\u671f\u5ba1\u8ba1\u65f6&#xff0c;\u589e\u91cf\u4e8b\u4ef6\u65e5\u5fd7\u6216\u5468\u671f checkpoint \u53ef\u80fd\u6bd4\u5728\u5185\u5b58\u4e2d\u4fdd\u7559\u6240\u6709\u96c6\u5408\u6839\u66f4\u5408\u9002\u3002<\/p>\n<h3 id=\"\u5341\u4e09-\u4e0e-dictionary-concurrentdictionary-\u548c-frozendictionary-\u7684\u533a\u522b\">\u5341\u4e09\u3001\u4e0e Dictionary\u3001ConcurrentDictionary \u548c FrozenDictionary \u7684\u533a\u522b<\/h3>\n<table>\n<tr>\u5bb9\u5668\u66f4\u65b0\u6a21\u578b\u5e76\u53d1\u8bfb\/\u5199\u5feb\u7167\u9002\u5408\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>Dictionary&lt;TKey,TValue&gt;<\/td>\n<td>\u539f\u5730\u53ef\u53d8<\/td>\n<td>\u5199\u9700\u5916\u90e8\u540c\u6b65<\/td>\n<td>\u9700\u590d\u5236<\/td>\n<td>\u5355\u6240\u6709\u8005\u3001\u9ad8\u9891\u53ef\u53d8\u66f4\u65b0<\/td>\n<\/tr>\n<tr>\n<td>ConcurrentDictionary&lt;TKey,TValue&gt;<\/td>\n<td>\u5171\u4eab\u53ef\u53d8<\/td>\n<td>\u63d0\u4f9b\u5e76\u53d1\u539f\u5b50 API<\/td>\n<td>\u679a\u4e3e\u8bed\u4e49\u9700\u6309\u5951\u7ea6\u7406\u89e3<\/td>\n<td>\u591a\u7ebf\u7a0b\u6301\u7eed\u6309\u952e\u66f4\u65b0<\/td>\n<\/tr>\n<tr>\n<td>ImmutableDictionary&lt;TKey,TValue&gt;<\/td>\n<td>\u8fd4\u56de\u65b0\u6839<\/td>\n<td>\u5df2\u53d1\u5e03\u5feb\u7167\u53ef\u5e76\u53d1\u8bfb&#xff0c;\u5199\u8005\u9700\u534f\u8c03<\/td>\n<td>\u539f\u751f\u7248\u672c<\/td>\n<td>\u8bfb\u591a\u5199\u5c11\u3001\u539f\u5b50\u5feb\u7167\u3001undo<\/td>\n<\/tr>\n<tr>\n<td>FrozenDictionary&lt;TKey,TValue&gt;<\/td>\n<td>\u6784\u5efa\u540e\u51bb\u7ed3<\/td>\n<td>\u53ea\u8bfb\u53d1\u5e03<\/td>\n<td>\u6ca1\u6709\u589e\u91cf\u65b0\u7248\u672c API<\/td>\n<td>\u6784\u5efa\u4e00\u6b21\u3001\u957f\u671f\u67e5\u8be2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>FrozenDictionary\u00a0\u9762\u5411\u201c\u51c6\u5907\u9636\u6bb5\u53ef\u4ee5\u4ed8\u51fa\u6784\u5efa\u6210\u672c&#xff0c;\u4e4b\u540e\u5927\u91cf\u53ea\u8bfb\u67e5\u8be2\u201d&#xff0c;\u4e0d\u662f\u53ef\u6301\u4e45\u5316\u66f4\u65b0\u6811\u3002\u5b83\u7684 API \u53ef\u7528\u6027\u548c\u5b9e\u73b0\u4f18\u5316\u8981\u67e5\u76ee\u6807 .NET \u7248\u672c\u3002\u6bcf\u6b21\u6570\u636e\u53d8\u5316\u90fd\u91cd\u5efa Frozen \u5bb9\u5668&#xff0c;\u53ef\u80fd\u8ba9\u6784\u5efa\u6210\u672c\u4e3b\u5bfc\u3002<\/p>\n<p>ConcurrentDictionary\u00a0\u9002\u5408\u591a\u5199\u8005\u5bf9\u5171\u4eab\u5f53\u524d\u72b6\u6001\u505a\u6309\u952e\u66f4\u65b0&#xff0c;\u4f46\u4e00\u7cfb\u5217\u4e0d\u540c\u952e\u7684\u66f4\u65b0\u4e0d\u81ea\u52a8\u53d8\u6210\u4e00\u4e2a\u539f\u5b50\u5feb\u7167\u3002ImmutableDictionary\u00a0\u53ef\u4ee5\u5728\u79c1\u6709\u65b0\u6839\u4e0a\u5b8c\u6210\u591a\u952e\u53d8\u66f4\u540e\u4e00\u6b21\u53d1\u5e03&#xff0c;\u4f46\u5199\u8005\u4e4b\u95f4\u5fc5\u987b\u7528 CAS \u91cd\u8bd5\u6216\u4e32\u884c\u6240\u6709\u6743\u89e3\u51b3\u66f4\u65b0\u4e22\u5931\u3002<\/p>\n<h3 id=\"\u5341\u56db-unity-\u8fb9\u754c\">\u5341\u56db\u3001Unity \u8fb9\u754c<\/h3>\n<p>Unity \u9879\u76ee\u9996\u5148\u8981\u6838\u5bf9\u76ee\u6807 Editor \u7248\u672c\u3001API Compatibility Level\u3001\u5f15\u7528\u7a0b\u5e8f\u96c6\u3001\u5305\u7248\u672c\u3001\u540e\u7aef\u548c\u5e73\u53f0\u3002\u684c\u9762\u5f53\u524d .NET SDK \u652f\u6301\u67d0\u4e2a\u00a0System.Collections.Immutable\u00a0API&#xff0c;\u4e0d\u4ee3\u8868\u76ee\u6807 Unity Mono\/IL2CPP Player \u4e2d\u5b58\u5728\u540c\u4e00 API \u8868\u9762\u548c\u540c\u4e00\u79c1\u6709\u5b9e\u73b0\u3002\u5e94\u7528\u6700\u5c0f asmdef \u63a2\u9488\u7f16\u8bd1\u5e76\u5728\u76ee\u6807\u8bbe\u5907\u6784\u5efa\u8fd0\u884c&#xff0c;\u4e0d\u8981\u628a CoreCLR \u6e90\u7801\u76f4\u63a5\u89c6\u4e3a Unity \u6e90\u7801\u3002<\/p>\n<p>\u4e0d\u53ef\u53d8\u5feb\u7167\u9002\u5408\u5c06\u7eaf\u6258\u7ba1\u914d\u7f6e\u3001\u5bfb\u8def\u56fe\u7d22\u5f15\u6216\u6e38\u620f\u89c4\u5219\u8868\u53d1\u5e03\u7ed9\u5de5\u4f5c\u7ebf\u7a0b\u3002\u4f46\u96c6\u5408\u4e0d\u53ef\u53d8\u4e0d\u4f1a\u8ba9\u00a0GameObject\u3001Transform\u3001Texture\u00a0\u7b49 Unity \u5bf9\u8c61\u53d8\u6210\u53ef\u4ece\u540e\u53f0\u7ebf\u7a0b\u8bbf\u95ee&#xff0c;\u4e5f\u4e0d\u4f1a\u89e3\u51b3\u539f\u751f\u5bf9\u8c61\u9500\u6bc1\u540e\u6258\u7ba1\u5305\u88c5\u5668\u7684\u751f\u547d\u5468\u671f\u3002\u5feb\u7167\u7684\u952e\u503c\u5e94\u662f\u53ef\u8de8\u7ebf\u7a0b\u5b89\u5168\u8bfb\u53d6\u7684\u7eaf\u6570\u636e\u3002<\/p>\n<p>\u8282\u70b9\u6811\u548c\u8def\u5f84\u590d\u5236\u4f1a\u4ea7\u751f\u6258\u7ba1\u5206\u914d\u3002\u5728\u6bcf\u5e27\u66f4\u65b0\u6210\u5343\u4e0a\u4e07\u952e\u7684\u70ed\u8def\u5f84\u4e2d&#xff0c;\u4e0d\u80fd\u4ec5\u56e0\u4e3a\u201c\u7ed3\u6784\u5171\u4eab\u201d\u5c31\u5047\u8bbe GC \u53ef\u5ffd\u7565\u3002\u5bf9\u6bcf\u4e2a\u76ee\u6807\u8bbe\u5907\u7684 Player \u5b9e\u6d4b\u66f4\u65b0\u6279\u6b21\u3001\u5206\u914d\u3001\u6258\u7ba1\u5806\u3001\u5e27\u65f6\u95f4\u4e0e\u9a7b\u7559\u65e7\u7248\u672c\u6570\u91cf\u3002\u5982\u679c\u53ea\u9700\u8981\u4e3b\u7ebf\u7a0b\u53ef\u53d8\u72b6\u6001&#xff0c;\u666e\u901a\u00a0Dictionary\u00a0\u4e0e\u660e\u786e\u5e27\u8fb9\u754c\u53ef\u80fd\u66f4\u7b80\u5355\u3002<\/p>\n<h3 id=\"\u5341\u4e94-\u6545\u969c\u53cd\u4f8b\">\u5341\u4e94\u3001\u6545\u969c\u53cd\u4f8b<\/h3>\n<p>\u53cd\u4f8b\u4e00&#xff1a;\u5ffd\u7565\u8fd4\u56de\u503c\u3002<\/p>\n<p>var map &#061; ImmutableDictionary&lt;string, int&gt;.Empty;<br \/>\nmap.Add(&#034;score&#034;, 10); \/\/ \u9519\u8bef&#xff1a;\u65b0\u5b57\u5178\u88ab\u4e22\u5f03<\/p>\n<p>\u6240\u6709\u4fee\u6539\u64cd\u4f5c\u90fd\u8fd4\u56de\u65b0\u5bb9\u5668&#xff0c;\u5fc5\u987b\u4fdd\u5b58\u00a0map &#061; map.Add(&#8230;)&#xff0c;\u6216\u8005\u5728\u8868\u8fbe\u5f0f\u94fe\u4e2d\u4f7f\u7528\u7ed3\u679c\u3002\u7f16\u8bd1\u5668\u4e0d\u4f1a\u56e0\u4e3a\u4f60\u5ffd\u7565\u4e86\u8fd4\u56de\u503c\u800c\u62a5\u9519\u3002<\/p>\n<p>\u53cd\u4f8b\u4e8c&#xff1a;\u5c06 Builder \u53d1\u5e03\u7ed9\u8bfb\u8005\u3002\u00a0Builder \u662f\u53ef\u53d8\u6279\u5904\u7406\u5de5\u5177&#xff0c;\u591a\u7ebf\u7a0b\u65e0\u540c\u6b65\u8bfb\u5199\u4f1a\u7834\u574f\u5b89\u5168\u8fb9\u754c\u3002\u53d1\u5e03\u00a0ToImmutable()\u00a0\u8fd4\u56de\u7684\u5feb\u7167&#xff0c;\u800c\u4e0d\u662f Builder\u3002<\/p>\n<p>\u53cd\u4f8b\u4e09&#xff1a;\u503c\u662f\u53ef\u53d8\u5bf9\u8c61\u5374\u58f0\u79f0\u5feb\u7167\u5b8c\u5168\u9694\u79bb\u3002\u00a0\u5bb9\u5668\u53ea\u51bb\u7ed3\u6620\u5c04\u5173\u7cfb&#xff0c;\u4e0d\u4f1a\u6df1\u62f7\u8d1d\u3002\u8bbe\u8ba1\u4e0d\u53ef\u53d8\u503c\u6a21\u578b\u6216\u5728\u53d1\u5e03\u8fb9\u754c\u590d\u5236\u3002<\/p>\n<p>\u53cd\u4f8b\u56db&#xff1a;\u591a\u5199\u8005\u7528\u666e\u901a\u8d4b\u503c\u66ff\u6362\u6839\u3002\u00a0\u4e24\u4e2a\u5199\u8005\u57fa\u4e8e\u540c\u4e00\u65e7\u6839\u751f\u6210\u65b0\u6839&#xff0c;\u540e\u8d4b\u503c\u4f1a\u4e22\u5931\u524d\u66f4\u65b0\u3002\u4f7f\u7528\u5355\u5199\u8005\u3001\u9501\u6216\u539f\u5b50 CAS \u5e2e\u52a9 API\u3002<\/p>\n<p>\u53cd\u4f8b\u4e94&#xff1a;\u4f9d\u8d56\u679a\u4e3e\u987a\u5e8f\u751f\u6210\u7f51\u7edc\u7b7e\u540d\u3002\u00a0\u54c8\u5e0c\u96c6\u5408\u987a\u5e8f\u4e0d\u662f\u8de8\u7248\u672c\u89c4\u8303\u3002\u5728\u7b7e\u540d\u7f16\u7801\u4e2d\u6309\u660e\u786e\u7684\u7a33\u5b9a\u6bd4\u8f83\u5668\u6392\u5e8f&#xff0c;\u5e76\u56fa\u5b9a\u6570\u5b57\u3001\u6587\u672c\u4e0e\u8f6c\u4e49\u683c\u5f0f\u3002<\/p>\n<h3 id=\"\u5341\u516d-\u53ef\u590d\u73b0\u6b63\u786e\u6027\u4e0e\u6027\u80fd\u5b9e\u9a8c\">\u5341\u516d\u3001\u53ef\u590d\u73b0\u6b63\u786e\u6027\u4e0e\u6027\u80fd\u5b9e\u9a8c<\/h3>\n<p>\u6b63\u786e\u6027\u6d4b\u8bd5\u5e94\u5148\u8986\u76d6\u6301\u4e45\u5316\u8bed\u4e49&#xff1a;\u751f\u6210 v0&#xff0c;\u9010\u6b65 Add\/SetItem\/Remove \u5f97\u5230 v1\u3001v2\u3001v3&#xff0c;\u6bcf\u4e00\u6b65\u90fd\u4fdd\u7559\u65e7\u5f15\u7528&#xff0c;\u6700\u540e\u9a8c\u8bc1\u6bcf\u4e2a\u7248\u672c\u7684\u5185\u5bb9\u4e0e\u5f53\u65f6\u671f\u671b\u5b8c\u5168\u76f8\u540c\u3002\u540c\u65f6\u5bf9\u7167\u4e00\u4e2a\u7b80\u5355\u7684\u53ef\u53d8\u00a0Dictionary\u00a0\u526f\u672c\u4f5c\u4e3a\u53c2\u8003\u6a21\u578b\u3002<\/p>\n<p>\u6bd4\u8f83\u5668\u6d4b\u8bd5\u5e94\u5305\u542b&#xff1a;\u5927\u5c0f\u5199\u7b49\u4ef7\u952e&#xff1b;\u4e0d\u540c\u952e\u8fd4\u56de\u76f8\u540c\u54c8\u5e0c\u7801&#xff1b;\u503c\u4e3a\u00a0default&#xff1b;\u5220\u9664\u51b2\u7a81\u6876\u7684\u9996\u9879\u3001\u4e2d\u95f4\u9879\u548c\u6700\u540e\u4e00\u9879&#xff1b;\u4ece\u591a\u5143\u7d20\u6876\u5220\u81f3\u7a7a\u6876&#xff1b;\u7528 Builder \u6279\u91cf\u66f4\u65b0\u540e\u786e\u8ba4\u65e7\u5feb\u7167\u672a\u53d8\u3002\u8fd8\u5e94\u7528\u6545\u610f\u5e38\u91cf\u54c8\u5e0c\u6bd4\u8f83\u5668\u8986\u76d6\u6700\u574f\u51b2\u7a81\u8def\u5f84&#xff0c;\u4f46\u4e0d\u8981\u628a\u5b83\u7684\u6027\u80fd\u5f53\u4f5c\u6b63\u5e38\u8d1f\u8f7d\u7ed3\u8bba\u3002<\/p>\n<p>public sealed class ConstantHashComparer : IEqualityComparer&lt;int&gt;<br \/>\n{<br \/>\n    public bool Equals(int x, int y) &#061;&gt; x &#061;&#061; y;<br \/>\n    public int GetHashCode(int value) &#061;&gt; 0;<br \/>\n}<\/p>\n<p>\u5e76\u53d1\u53d1\u5e03\u6d4b\u8bd5\u5e94\u8ba9\u591a\u4e2a\u8bfb\u8005\u6301\u7eed\u8bfb\u53d6\u6839\u5feb\u7167&#xff0c;\u9a8c\u8bc1\u8de8\u591a\u952e\u7684\u4e1a\u52a1\u4e0d\u53d8\u5f0f&#xff1b;\u5199\u8005\u5728\u5c40\u90e8\u5b8c\u6210\u6574\u6279\u53d8\u66f4\u540e\u53d1\u5e03\u3002\u5bf9\u591a\u5199\u8005 CAS \u5faa\u73af&#xff0c;\u8bb0\u5f55\u91cd\u8bd5\u6b21\u6570&#xff0c;\u9a8c\u8bc1\u6bcf\u4e2a\u903b\u8f91\u66f4\u65b0\u6070\u597d\u4e00\u6b21\u51fa\u73b0&#xff0c;\u5e76\u4fdd\u8bc1\u91cd\u7b97\u51fd\u6570\u65e0\u4e0d\u53ef\u91cd\u590d\u526f\u4f5c\u7528\u3002<\/p>\n<p>\u6027\u80fd\u5b9e\u9a8c\u5e94\u533a\u5206&#xff1a;\u5355\u9879\u66f4\u65b0\u3001\u6279\u91cf Builder \u66f4\u65b0\u3001\u7a33\u6001\u67e5\u627e\u3001\u5168\u91cf\u679a\u4e3e\u3001\u4fdd\u7559\u591a\u4e2a\u65e7\u7248\u672c&#xff0c;\u4ee5\u53ca\u5168\u90e8\u4e22\u5f03\u65e7\u7248\u672c\u3002\u540c\u65f6\u8bb0\u5f55\u65f6\u95f4\u3001\u5206\u914d\u5b57\u8282\u3001GC \u6b21\u6570\u4e0e\u9a7b\u7559\u5806&#xff0c;\u56fa\u5b9a SDK\/runtime\u3001CPU\u3001\u952e\u5206\u5e03\u3001\u6bd4\u8f83\u5668\u3001\u521d\u59cb\u89c4\u6a21\u3001\u66f4\u65b0\u6279\u6b21\u548c\u968f\u673a\u79cd\u5b50\u3002<\/p>\n<p>\u5bf9\u6bd4\u00a0Dictionary\u3001ConcurrentDictionary\u3001ImmutableDictionary\u00a0\u548c\u76ee\u6807\u7248\u672c\u652f\u6301\u7684 Frozen \u5bb9\u5668\u65f6&#xff0c;\u5fc5\u987b\u4f7f\u7528\u7b49\u4ef7\u4e1a\u52a1\u5951\u7ea6\u3002\u5982\u679c\u4e00\u4e2a\u65b9\u6848\u6bcf\u6b21\u4fdd\u7559\u7248\u672c&#xff0c;\u53e6\u4e00\u4e2a\u65b9\u6848\u5c31\u5730\u66f4\u65b0\u4e14\u4e0d\u63d0\u4f9b\u5feb\u7167&#xff0c;\u5355\u7eaf\u5bf9\u6bd4\u6bcf\u79d2\u64cd\u4f5c\u6570\u4e0d\u662f\u516c\u5e73\u6bd4\u8f83\u3002\u4e0d\u8981\u53d1\u5e03\u6ca1\u6709\u539f\u59cb\u62a5\u544a\u548c\u53ef\u8fd0\u884c\u4ee3\u7801\u7684\u56fa\u5b9a\u500d\u6570\u3002<\/p>\n<h3 id=\"\u5341\u4e03-\u6e90\u7801\u5ba1\u67e5\u6e05\u5355\">\u5341\u4e03\u3001\u6e90\u7801\u5ba1\u67e5\u6e05\u5355<\/h3>\n<p>\u9605\u8bfb\u4e00\u4e2a\u7279\u5b9a\u7248\u672c\u7684\u6e90\u7801\u65f6&#xff0c;\u53ef\u4f9d\u6b21\u95ee&#xff1a;<\/p>\n<li>\u4ee3\u7801\u5c5e\u4e8e\u54ea\u4e2a\u00a0dotnet\/runtime\u00a0tag\/commit&#xff0c;\u76ee\u6807\u5e94\u7528\u5b9e\u9645\u52a0\u8f7d\u54ea\u4e2a\u7a0b\u5e8f\u96c6\u7248\u672c&#xff1f;<\/li>\n<li>\u4e00\u7ea7\u6811\u8282\u70b9\u7684\u6574\u6570\u952e\u662f\u4ec0\u4e48&#xff0c;\u5b83\u5982\u4f55\u5e73\u8861&#xff0c;\u7a7a\u8282\u70b9\u5982\u4f55\u8868\u793a&#xff1f;<\/li>\n<li>HashBucket \u5982\u4f55\u533a\u5206\u7b2c\u4e00\u9879\u548c\u989d\u5916\u51b2\u7a81\u9879&#xff0c;\u6876\u672c\u8eab\u4f7f\u7528\u4ec0\u4e48\u6301\u4e45\u5316\u7ed3\u6784&#xff1f;<\/li>\n<li>\u952e\u6bd4\u8f83\u5668\u548c\u503c\u6bd4\u8f83\u5668\u5206\u522b\u7528\u5728\u54ea\u4e9b API \u5206\u652f&#xff1f;<\/li>\n<li>Add\u3001SetItem\u3001Remove \u5982\u4f55\u533a\u5206\u65e0\u53d8\u5316\u3001\u6dfb\u52a0\u3001\u66f4\u65b0\u4e0e\u51b2\u7a81&#xff1f;<\/li>\n<li>\u6811\u8def\u5f84\u4f55\u65f6\u590d\u5236&#xff0c;\u54ea\u4e9b\u5b50\u6811\u76f4\u63a5\u5171\u4eab&#xff0c;\u5e73\u8861\u65cb\u8f6c\u4f1a\u521b\u5efa\u54ea\u4e9b\u8282\u70b9&#xff1f;<\/li>\n<li>Builder \u5982\u4f55\u6807\u8bc6\u8282\u70b9\u6240\u6709\u6743\/\u51bb\u7ed3\u72b6\u6001&#xff0c;ToImmutable\u00a0\u540e\u7ee7\u7eed\u4fee\u6539\u4e3a\u4f55\u4e0d\u6c61\u67d3\u65e7\u5feb\u7167&#xff1f;<\/li>\n<li>\u679a\u4e3e\u5668\u5982\u4f55\u7a7f\u8fc7\u6811\u4e0e\u6876&#xff0c;\u662f\u5426\u6709\u7ed3\u6784\u4f53\u679a\u4e3e\u548c\u63a5\u53e3\u679a\u4e3e\u7684\u4e0d\u540c\u8def\u5f84&#xff1f;<\/li>\n<li>\u66f4\u6362\u6bd4\u8f83\u5668\u65f6\u662f\u5426\u9700\u8981\u91cd\u5efa&#xff0c;\u65b0\u6bd4\u8f83\u5668\u4f7f\u65e7\u952e\u7b49\u4ef7\u65f6\u5982\u4f55\u5904\u7406&#xff1f;<\/li>\n<li>\u54ea\u4e9b\u7ed3\u8bba\u662f\u516c\u5171\u5951\u7ea6&#xff0c;\u54ea\u4e9b\u53ea\u662f\u8be5 tag \u7684\u5206\u914d\u6216\u5feb\u8def\u5f84\u4f18\u5316&#xff1f;<\/li>\n<h3 id=\"\u7ed3\u8bed\">\u7ed3\u8bed<\/h3>\n<p>ImmutableDictionary\u00a0\u548c\u00a0ImmutableHashSet\u00a0\u7684\u6838\u5fc3\u4e0d\u662f\u4e00\u4e2a\u53ef\u4ee5\u4ece\u201c\u6301\u4e45\u5316\u54c8\u5e0c\u5bb9\u5668\u201d\u731c\u51fa\u6765\u7684 HAMT \u6807\u7b7e\u3002\u5728\u53ef\u6838\u9a8c\u7684\u76ee\u6807 .NET \u5b9e\u73b0\u4e2d&#xff0c;\u5b83\u4eec\u901a\u8fc7\u6309 32 \u4f4d\u54c8\u5e0c\u7801\u7ec4\u7ec7\u7684\u5e73\u8861\u6811\u5b9a\u4f4d\u6876&#xff0c;\u7528\u6876\u5185\u76f8\u7b49\u6bd4\u8f83\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81&#xff0c;\u901a\u8fc7\u8def\u5f84\u590d\u5236\u548c\u672a\u53d8\u5b50\u6811\u5171\u4eab\u4fdd\u7559\u65e7\u7248\u672c\u3002<\/p>\n<p>\u4e0d\u53ef\u53d8\u5feb\u7167\u5e26\u6765\u7684\u4e3b\u8981\u6536\u76ca\u662f\u6e05\u6670\u7684\u53d1\u5e03\u8fb9\u754c&#xff1a;\u8bfb\u8005\u62ff\u5230\u4e00\u4e2a\u6839\u5c31\u80fd\u7a33\u5b9a\u904d\u5386&#xff0c;\u5199\u8005\u53ef\u4ee5\u5728\u79c1\u6709\u65b0\u6839\u4e0a\u5b8c\u6210\u591a\u6b65\u66f4\u65b0\u540e\u4e00\u6b21\u53d1\u5e03\u3002\u4f46\u5b83\u4e0d\u4f1a\u6df1\u5ea6\u51bb\u7ed3\u952e\u503c\u5bf9\u8c61&#xff0c;\u4e0d\u4f1a\u81ea\u52a8\u89e3\u51b3\u591a\u5199\u8005\u66f4\u65b0\u4e22\u5931&#xff0c;\u4e5f\u4e0d\u4f1a\u6d88\u9664\u8282\u70b9\u5206\u914d\u3001GC \u548c\u6700\u574f\u54c8\u5e0c\u51b2\u7a81\u3002<\/p>\n<p>\u6b63\u786e\u9009\u578b\u9700\u8981\u56de\u5230\u4e1a\u52a1\u8bed\u4e49&#xff1a;\u9700\u8981\u9ad8\u9891\u539f\u5730\u66f4\u65b0\u5c31\u8bc4\u4f30\u666e\u901a\u6216\u5e76\u53d1\u5b57\u5178&#xff0c;\u6784\u5efa\u4e00\u6b21\u957f\u671f\u53ea\u8bfb\u5c31\u8bc4\u4f30 Frozen \u5bb9\u5668&#xff0c;\u9700\u8981\u7248\u672c\u3001\u539f\u5b50\u5feb\u7167\u548c\u8bfb\u8005\u9694\u79bb\u65f6\u518d\u9009\u4e0d\u53ef\u53d8\u5bb9\u5668\u3002\u6700\u540e\u7528\u56fa\u5b9a runtime tag \u6838\u5bf9\u6e90\u7801&#xff0c;\u7528\u574f\u54c8\u5e0c\u548c\u5e76\u53d1\u53d1\u5e03\u6d4b\u8bd5\u8fb9\u754c&#xff0c;\u7528\u771f\u5b9e\u66f4\u65b0\u6279\u6b21\u548c\u7248\u672c\u4fdd\u7559\u6a21\u5f0f\u6d4b\u91cf\u6210\u672c\u3002<\/p>\n<hr \/>\n<p>\u4e0b\u4e00\u7bc7&#xff1a;\u4e0d\u53ef\u53d8\u96c6\u5408\u7efc\u5408\u5bf9\u6bd4\u4e0e\u9009\u578b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ImmutableDictionary \u4e0e ImmutableHashSet&#xff1a;\u54c8\u5e0c\u6876\u6811\u3001\u7ed3\u6784\u5171\u4eab\u4e0e\u539f\u5b50\u5feb\u7167 \u7cfb\u5217&#xff1a;C# \u4e0e\u5e38\u7528\u6570\u636e\u7ed3\u6784\u6e90\u7801\u5256\u6790 \u00b7 \u4e0d\u53ef\u53d8\u96c6\u5408\u7bc7 \u9605\u8bfb\u65f6\u95f4&#xff1a;\u7ea6 60 \u5206\u949f \u524d\u7f6e\u77e5\u8bc6&#xff1a;\u54c8\u5e0c\u8868\u3001\u5e73\u8861\u6811\u3001\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784\u3001\u6bd4\u8f83\u5668 \u6e90\u7801\u57fa\u7ebf&#xff1a;System.Collections.Immutable\u00a08.0.0&#xff0c;\u5bf9\u5e94\u00a0dotnet\/runtime v8.0.0\u00a0\/ commit\u00a05535e<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11901,55,3520,1813,427],"topic":[],"class_list":["post-104648","post","type-post","status-publish","format-standard","hentry","category-server","tag-immutable","tag-c","tag-3520","tag-1813","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/104648.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"ImmutableDictionary \u4e0e ImmutableHashSet&#xff1a;\u54c8\u5e0c\u6876\u6811\u3001\u7ed3\u6784\u5171\u4eab\u4e0e\u539f\u5b50\u5feb\u7167 \u7cfb\u5217&#xff1a;C# \u4e0e\u5e38\u7528\u6570\u636e\u7ed3\u6784\u6e90\u7801\u5256\u6790 \u00b7 \u4e0d\u53ef\u53d8\u96c6\u5408\u7bc7 \u9605\u8bfb\u65f6\u95f4&#xff1a;\u7ea6 60 \u5206\u949f \u524d\u7f6e\u77e5\u8bc6&#xff1a;\u54c8\u5e0c\u8868\u3001\u5e73\u8861\u6811\u3001\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784\u3001\u6bd4\u8f83\u5668 \u6e90\u7801\u57fa\u7ebf&#xff1a;System.Collections.Immutable\u00a08.0.0&#xff0c;\u5bf9\u5e94\u00a0dotnet\/runtime v8.0.0\u00a0\/ commit\u00a05535e\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/104648.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-12T13:57:12+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/104648.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/104648.html\",\"name\":\"08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-12T13:57:12+00:00\",\"dateModified\":\"2026-09-12T13:57:12+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/104648.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/104648.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/104648.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/104648.html","og_locale":"zh_CN","og_type":"article","og_title":"08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"ImmutableDictionary \u4e0e ImmutableHashSet&#xff1a;\u54c8\u5e0c\u6876\u6811\u3001\u7ed3\u6784\u5171\u4eab\u4e0e\u539f\u5b50\u5feb\u7167 \u7cfb\u5217&#xff1a;C# \u4e0e\u5e38\u7528\u6570\u636e\u7ed3\u6784\u6e90\u7801\u5256\u6790 \u00b7 \u4e0d\u53ef\u53d8\u96c6\u5408\u7bc7 \u9605\u8bfb\u65f6\u95f4&#xff1a;\u7ea6 60 \u5206\u949f \u524d\u7f6e\u77e5\u8bc6&#xff1a;\u54c8\u5e0c\u8868\u3001\u5e73\u8861\u6811\u3001\u6301\u4e45\u5316\u6570\u636e\u7ed3\u6784\u3001\u6bd4\u8f83\u5668 \u6e90\u7801\u57fa\u7ebf&#xff1a;System.Collections.Immutable\u00a08.0.0&#xff0c;\u5bf9\u5e94\u00a0dotnet\/runtime v8.0.0\u00a0\/ commit\u00a05535e","og_url":"https:\/\/www.wsisp.com\/helps\/104648.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-12T13:57:12+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/104648.html","url":"https:\/\/www.wsisp.com\/helps\/104648.html","name":"08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-12T13:57:12+00:00","dateModified":"2026-09-12T13:57:12+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/104648.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/104648.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/104648.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"08-03-\u4e0d\u53ef\u53d8-ImmutableDictionary-TKey-TValue-\u4e0eImmutableHashSet-T-\u6301\u4e45\u5316\u54c8\u5e0c\u6811"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/104648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=104648"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/104648\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=104648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=104648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=104648"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=104648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}