{"id":781,"date":"2025-04-21T15:04:33","date_gmt":"2025-04-21T07:04:33","guid":{"rendered":"https:\/\/www.hyy.net\/?p=781"},"modified":"2025-04-21T15:04:33","modified_gmt":"2025-04-21T07:04:33","slug":"csharp-dictionary","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=781","title":{"rendered":"C#\u4e4b Dictionary \u8be6\u89e3"},"content":{"rendered":"<p>C#\u4e4b Dictionary \u8be6\u89e3<\/p>\n<h2>\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Dictionary&lt;TKey, TValue&gt;\u662fC#\u4e2d\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u96c6\u5408\u7684\u6cdb\u578b\u7c7b\uff0c\u5c5e\u4e8eSystem.Collections.Generic\u547d\u540d\u7a7a\u95f4\u3002\u5b83\u5141\u8bb8\u4f7f\u7528\u952e\uff08Key\uff09\u6765\u8bbf\u95ee\u4e0e\u5176\u5173\u8054\u7684\u503c\uff08Value\uff09\u3002\u5176\u4e2d\uff0cTKey\u8868\u793a\u5b57\u5178\u4e2d\u952e\u7684\u7c7b\u578b\uff0cTValue\u8868\u793a\u5b57\u5178\u4e2d\u503c\u7684\u7c7b\u578b\u3002<\/p>\n<h2>Dictionary\u7684\u57fa\u672c\u7ed3\u6784<\/h2>\n<ul>\n<li>\u952e\uff08Key\uff09\uff1a\u552f\u4e00\u6807\u8bc6\u96c6\u5408\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u952e\u662f\u552f\u4e00\u7684\uff0c\u4e0d\u80fd\u6709\u91cd\u590d\u3002<\/li>\n<li>\u503c\uff08Value\uff09\uff1a\u4e0e\u952e\u76f8\u5173\u8054\u7684\u6570\u636e\u3002\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\uff0c\u5e76\u4e14\u53ef\u4ee5\u6709\u91cd\u590d\u3002<\/li>\n<li>\u952e\u503c\u5bf9\uff08KeyValuePair\uff09\uff1a\u952e\u548c\u503c\u7684\u7ec4\u5408\uff0c\u8868\u793aDictionary\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ul>\n<h2>Dictionary\u7684\u4e3b\u8981\u7279\u6027<\/h2>\n<ul>\n<li>\u5feb\u901f\u8bbf\u95ee\uff1a\u901a\u8fc7\u952e\u53ef\u4ee5\u5feb\u901f\u68c0\u7d22\u5230\u5bf9\u5e94\u7684\u503c\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u63a5\u8fd1O(1)\uff0c\u56e0\u4e3aDictionary&lt;TKey,TValue&gt;\u7c7b\u662f\u4f5c\u4e3a\u54c8\u5e0c\u8868\u5b9e\u73b0\u3002<\/li>\n<li>\u552f\u4e00\u952e\uff08Key\uff09\uff1a\u6bcf\u4e2a\u952e\u5728Dictionary\u4e2d\u90fd\u662f\u552f\u4e00\u7684\uff0c\u4e0d\u80fd\u91cd\u590d\u3002<\/li>\n<li>\u52a8\u6001\u5927\u5c0f\uff1aDictionary\u7684\u5927\u5c0f\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\uff0c\u5f53\u5143\u7d20\u6570\u91cf\u8d85\u8fc7\u5bb9\u91cf\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u6269\u5bb9\u3002<\/li>\n<li>\u65e0\u5e8f\u96c6\u5408\uff1aDictionary\u4e2d\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u7684\uff0c\u4e0d\u80fd\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5b83\u4eec\u3002<\/li>\n<\/ul>\n<h2>Dictionary\u7684\u5e38\u7528\u64cd\u4f5c<\/h2>\n<p>\u4ee5\u4e0b\u662fC#\u4e2dDictionary\u7684\u5e38\u7528\u64cd\u4f5c\u5b8c\u6574\u4ee3\u7801\uff0c\u5176\u4e2d\u5305\u62ec\u6dfb\u52a0\u5143\u7d20\u3001\u8bbf\u95ee\u5143\u7d20\u3001\u4fee\u6539\u5143\u7d20\u3001\u5220\u9664\u5143\u7d20\u3001\u68c0\u67e5\u952e\u6216\u503c\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u53ca\u904d\u5386\u5143\u7d20\uff1a<\/p>\n<pre><code>public static void DictionaryOperation()\n{\n    \/\/\u521b\u5efa\u4e00\u4e2aDictionary\u6765\u5b58\u50a8\u5b66\u751f\u5b66\u53f7ID\u548c\u59d3\u540d\n    Dictionary&lt;int, string&gt; studentDic = new Dictionary&lt;int, string&gt;();\n\n    #region \u6dfb\u52a0\u5143\u7d20\n\n    \/\/ Add\u65b9\u6cd5\uff08\u952e\u5fc5\u987b\u552f\u4e00\uff09\n    studentDic.Add(1, &quot;\u5927\u59da&quot;);\n    studentDic.Add(2, &quot;\u5c0f\u8881&quot;);\n    studentDic.Add(3, &quot;Edwin&quot;);\n\n    \/\/ \u7d22\u5f15\u5668\u8bed\u6cd5\uff08\u952e\u4e0d\u5b58\u5728\u65f6\u6dfb\u52a0\uff0c\u5b58\u5728\u65f6\u66f4\u65b0\uff09\n    studentDic[4] = &quot;Charlie&quot;;\n    studentDic[5] = &quot;\u8ffd\u9010\u65f6\u5149\u8005&quot;;\n\n    \/\/ \u5b89\u5168\u6dfb\u52a0\uff08\u907f\u514d\u5f02\u5e38\uff09\n    bool isAdded = studentDic.TryAdd(6, &quot;\u5c0f\u660e&quot;); \/\/ \u8fd4\u56de false\uff0c\u56e0\u952e\u5df2\u5b58\u5728\n\n    #endregion\n\n    #region \u8bbf\u95ee\u5143\u7d20\n\n    \/\/ \u76f4\u63a5\u8bbf\u95ee\uff08\u952e\u5fc5\u987b\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u6709\u5f02\u5e38\uff09\n    var currentUserName = studentDic[1];\n    Console.WriteLine($&quot;\u5f53\u524d\u5b66\u751f\u59d3\u540d: {currentUserName}&quot;);\n\n    \/\/ \u5b89\u5168\u8bbf\u95ee\uff08\u907f\u514d\u5f02\u5e38\uff09\n    if (studentDic.TryGetValue(5, outvar getUserName))\n    {\n        Console.WriteLine($&quot;UserName\uff1a{getUserName}&quot;);\n    }\n    else\n    {\n        Console.WriteLine(&quot;\u5f53\u524d\u5b66\u751fID\u4e0d\u5b58\u5728&quot;);\n    }\n\n    #endregion\n\n    #region\n\n    \/\/ \u4fee\u6539\u5143\u7d20\n    studentDic[2] = &quot;\u5927\u897f\u74dc&quot;;\n\n    Console.WriteLine($&quot;\u4fee\u6539\u540e\u7684\u540d\u79f0\uff1a{studentDic[2]}&quot;);\n\n    #endregion\n\n    #region \u5220\u9664\u5143\u7d20\n\n    \/\/ \u5220\u9664\u5143\u7d20\n    bool isRemoved = studentDic.Remove(3);\n\n    Console.WriteLine($&quot;\u5220\u9664\u7ed3\u679c\uff1a{isRemoved}&quot;);\n\n    #endregion\n\n    #region \u68c0\u67e5\u952e\u6216\u503c\u662f\u5426\u5b58\u5728\n\n    \/\/ \u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\n    if (studentDic.ContainsKey(1))\n    {\n        Console.WriteLine(&quot;\u5b58\u5728&quot;);\n    }\n    else\n    {\n        Console.WriteLine(&quot;\u4e0d\u5b58\u5728&quot;);\n    }\n\n    bool isExistcontainsValue = studentDic.ContainsValue(&quot;\u8ffd\u9010\u65f6\u5149\u8005&quot;);\n\n    Console.WriteLine($&quot;\u662f\u5426\u5b58\u5728\uff1a{isExistcontainsValue}&quot;);\n\n    #endregion\n\n    #region \u904d\u5386\u5143\u7d20\n\n    \/\/ \u904d\u5386\u5143\u7d20\n    foreach (KeyValuePair&lt;int, string&gt; student in studentDic)\n    {\n        Console.WriteLine($&quot;ID: {student.Key}, Name: {student.Value}&quot;);\n    }\n\n    \/\/ \u4f7f\u7528\u952e\u7684\u679a\u4e3e\u5668\n    foreach (var key in studentDic.Keys)\n    {\n        Console.WriteLine($&quot;Key: {key}, Value: {studentDic[key]}&quot;);\n    }\n\n    \/\/ \u4f7f\u7528\u503c\u7684\u679a\u4e3e\u5668\n    foreach (varvaluein studentDic.Values)\n    {\n        \/\/ \u6ce8\u610f\uff1a\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u80fd\u76f4\u63a5\u83b7\u53d6\u952e\uff0c\u53ea\u80fd\u83b7\u53d6\u503c\n        Console.WriteLine($&quot;Value: {value}&quot;);\n    }\n\n    #endregion\n}<\/code><\/pre>\n<p>\u53c2\u8003\u6587\u7ae0<br \/>\n<a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.collections.generic.dictionary-2?view=net-9.0\">https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/api\/system.collections.generic.dictionary-2?view=net-9.0<\/a><\/p>\n<p><a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIxMTUzNzM5Ng==&amp;mid=2247510982&amp;idx=1&amp;sn=40926d914ed235d887ad0b5cde7db93f&amp;scene=21#wechat_redirect\">https:\/\/mp.weixin.qq.com\/s?__biz=MzIxMTUzNzM5Ng==&mid=2247510982&idx=1&sn=40926d914ed235d887ad0b5cde7db93f&scene=21#wechat_redirect<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C#\u4e4b Dictionary \u8be6\u89e3 \u57fa\u672c\u6982\u5ff5 Dictionary&lt;TKey, TValue&gt;\u662fC#\u4e2d\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u96c6\u5408\u7684\u6cdb\u578b\u7c7b\uff0c\u5c5e\u4e8eSystem.Collections.Generic\u547d\u540d\u7a7a\u95f4\u3002\u5b83\u5141\u8bb8\u4f7f\u7528\u952e\uff08Key\uff09\u6765\u8bbf\u95ee\u4e0e\u5176\u5173\u8054\u7684\u503c\uff08Value\uff09\u3002\u5176\u4e2d\uff0cTKey\u8868\u793a\u5b57\u5178\u4e2d\u952e\u7684\u7c7b\u578b\uff0cTValue\u8868\u793a\u5b57\u5178\u4e2d\u503c\u7684\u7c7b\u578b\u3002 Dictionary\u7684\u57fa\u672c\u7ed3\u6784 \u952e\uff08Key\uff09\uff1a\u552f\u4e00\u6807\u8bc6\u96c6\u5408\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u952e\u662f\u552f\u4e00\u7684\uff0c\u4e0d\u80fd\u6709\u91cd\u590d\u3002 \u503c\uff08Value\uff09\uff1a\u4e0e\u952e\u76f8\u5173\u8054\u7684\u6570\u636e\u3002\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\uff0c\u5e76\u4e14\u53ef\u4ee5\u6709\u91cd\u590d\u3002 \u952e\u503c\u5bf9\uff08KeyValuePair\uff09\uff1a\u952e\u548c\u503c\u7684\u7ec4\u5408\uff0c\u8868\u793aDictionary\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002 Dictionary\u7684\u4e3b\u8981\u7279\u6027 \u5feb\u901f\u8bbf\u95ee\uff1a\u901a\u8fc7\u952e\u53ef\u4ee5\u5feb\u901f\u68c0\u7d22\u5230\u5bf9\u5e94\u7684\u503c\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u63a5\u8fd1O(1)\uff0c\u56e0\u4e3aDictionary&lt;TKey,TValue&gt;\u7c7b\u662f\u4f5c\u4e3a\u54c8\u5e0c\u8868\u5b9e\u73b0\u3002 \u552f\u4e00\u952e\uff08Key\uff09\uff1a\u6bcf\u4e2a\u952e\u5728Dictionary\u4e2d\u90fd\u662f\u552f\u4e00\u7684\uff0c\u4e0d\u80fd\u91cd\u590d\u3002 \u52a8\u6001\u5927\u5c0f\uff1aDictionary\u7684\u5927\u5c0f\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\uff0c\u5f53\u5143\u7d20\u6570\u91cf\u8d85\u8fc7\u5bb9\u91cf\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u6269\u5bb9\u3002 \u65e0\u5e8f\u96c6\u5408\uff1aDictionary\u4e2d\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u7684\uff0c\u4e0d\u80fd\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5b83\u4eec\u3002 Dictionary\u7684\u5e38\u7528\u64cd\u4f5c \u4ee5\u4e0b\u662fC#\u4e2dDictionary\u7684\u5e38\u7528\u64cd\u4f5c\u5b8c\u6574\u4ee3\u7801\uff0c\u5176\u4e2d\u5305\u62ec\u6dfb\u52a0\u5143\u7d20\u3001\u8bbf\u95ee\u5143\u7d20\u3001\u4fee\u6539\u5143\u7d20\u3001\u5220\u9664\u5143\u7d20\u3001\u68c0\u67e5\u952e\u6216\u503c\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u53ca\u904d\u5386\u5143\u7d20\uff1a public static void DictionaryOperation() { \/\/\u521b\u5efa\u4e00\u4e2aDictionary\u6765\u5b58\u50a8\u5b66\u751f\u5b66\u53f7ID\u548c\u59d3\u540d Dictionary&lt;int, string&gt; studentDic = new Dictionary&lt;int, string&gt;(); #region \u6dfb\u52a0\u5143\u7d20 \/\/ Add\u65b9\u6cd5\uff08\u952e\u5fc5\u987b\u552f\u4e00\uff09 studentDic.Add(1, &quot;\u5927\u59da&quot;); studentDic.Add(2, &quot;\u5c0f\u8881&quot;); studentDic.Add(3, &quot;Edwin&quot;); \/\/ \u7d22\u5f15\u5668\u8bed\u6cd5\uff08\u952e\u4e0d\u5b58\u5728\u65f6\u6dfb\u52a0\uff0c\u5b58\u5728\u65f6\u66f4\u65b0\uff09 studentDic[4] = &quot;Charlie&quot;; studentDic[5] = &quot;\u8ffd\u9010\u65f6\u5149\u8005&quot;; \/\/ \u5b89\u5168\u6dfb\u52a0\uff08\u907f\u514d\u5f02\u5e38\uff09 bool isAdded = studentDic.TryAdd(6, &quot;\u5c0f\u660e&quot;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-781","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=781"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/781\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}