{"id":2064,"date":"2021-09-21T05:54:22","date_gmt":"2021-09-20T21:54:22","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=2064"},"modified":"2022-02-16T21:45:43","modified_gmt":"2022-02-16T13:45:43","slug":"data-structure","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2021\/09\/21\/data-structure\/","title":{"rendered":"D-24 \u8cc7\u6599\u7d50\u69cb data structure ? List ? Dictionary"},"content":{"rendered":"<h1>\u9084\u6709\u4e00\u7a2e\u8ff4\u5708<\/h1>\n<p>\u6628\u5929\u4ecb\u7d39\u6642\u5927\u982d\u9084\u6709\u4e00\u500b\u8ff4\u5708\u7684\u65b9\u5f0f\u6c92\u6709\u4ecb\u7d39\uff0c\u70ba\u4ec0\u9ebc\u6628\u5929\u4e0d\u4ecb\u7d39\u5462\uff0c\u662f\u56e0\u70ba\u7bc7\u5e45\u95dc\u4fc2\u9084\u662f\u6709\u5176\u4ed6\u539f\u56e0\uff0c\u8b93\u6211\u5011\u770b\u4e0b\u53bb\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u672a\u4ecb\u7d39 foreach\u7684\u539f\u56e0<\/h2>\n<p>\u300c\u524d\u8f29\u65e9\uff0c\u8d95\u5feb\u4f86\u4ecb\u7d39\u6628\u5929\u6c92\u8b1b\u7684<code>foreach<\/code>\u5427\uff0c\u70ba\u5565\u6628\u5929\u8981\u8ce3\u95dc\u5b50\u5462\uff1f\u300d<br \/>\n\u4e00\u5927\u65e9\u5c0f\u5149\u5c31\u8feb\u4e0d\u6025\u5f85\u7684\u60f3\u8ddf\u5927\u982d\u8acb\u6559<code>foreach<\/code>\u7684\u4e8b\u60c5\uff0c\u807d\u5230\u9019\u500b\u5927\u982d\u4f9d\u820a\u5f9e\u6587\u4ef6\u4e2d\u62ac\u8d77\u982d\u4f86\uff0c\u7d93\u904e\u6628\u5929\u7684\u4f11\u606f\u5927\u982d\u7684\u9ed1\u773c\u5708\u4e0d\u898b\u4e86\uff0c\u53cd\u800c\u662f\u5c0f\u5149\u81c9\u4e0a\u591a\u4e86\u9ed1\u773c\u5708\u3002<br \/>\n\u300c\u597d\u5594\u6211\u5011\u4f86\u958b\u59cb\u8aaa\u660e\u4e00\u4e0b<code>foreach<\/code>\u56c9\uff0c\u4e0d\u904e\u4f60\u662f\u600e\u9ebc\u4e86\uff1f\u300d<br \/>\n\u7d50\u679c\u5c0f\u5149\u4e0d\u597d\u610f\u601d\u7684\u5df2\u7b11\u5e36\u904e\u3002\u63a5\u8457\u5927\u982d\u5c31\u958b\u59cb\u4ecb\u7d39<code>foreach<\/code>\u3002<\/p>\n<h3>foreach<\/h3>\n<p>\u7c21\u55ae\u8aaa\u8d77\u4f86<code>foreach<\/code>\u5c31\u662f\u5de1\u8a2a\u8b8a\u6578\u4e2d\u7684\u5143\u7d20\uff0c\u6240\u4ee5\u5beb\u8d77\u4f86\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"language-cs\">foreach(var item in list)\n{\n    \/\/ do something \n}<\/code><\/pre>\n<h3>in \u5f8c\u9762\u8981\u653e\u4ec0\u9ebc<\/h3>\n<p>\u300c\u524d\u8f29\uff0c\u6211\u6709\u4e00\u500b\u554f\u984c\uff0c\u95dc\u65bc\u4e0a\u8ff0\u4f8b\u5b50\u4e4b\u4e2d<code>in<\/code>\u5f8c\u9762\u8981\u653e\u4ec0\u9ebc\uff1f\u300d<br \/>\n\u5927\u982d\u807d\u5230\u9019\u554f\u984c\u8868\u73fe\u51fa\u8b9a\u8cde\u7684\u8868\u60c5\uff0c\u4e26\u4e14\u958b\u59cb\u8aaa\u660e\u9019\u500b\u554f\u984c\u3002<br \/>\n\u300c\u4e0d\u932f\uff0c\u5f88\u5c08\u5fc3\u5728\u807d\uff0c<code>in<\/code>\u5f8c\u9762\u653e\u7684\u5c31\u662f\u8b8a\u6578\u300d<br \/>\n\u300c\u86e4\u22ef\u8b8a\u6578\u300d<br \/>\n\u807d\u5230\u9019\u7b54\u6848\u8b93\u5c0f\u5149\u5f88\u8a1d\u7570\uff0c\u7136\u5f8c\u5927\u982d\u537b\u54c8\u54c8\u5927\u7b11\u8d77\u4f86\u3002<br \/>\n\u300c\u6c92\u932f\u662f\u8b8a\u6578\uff0c\u4f46\u662f\u4e0d\u662f\u666e\u901a\u7684\u8b8a\u6578\uff0c\u662f\u4e00\u7a2e\u53ef\u4ee5\u5de1\u8a2a\u7684\u8b8a\u6578\uff0c\u8b93\u6211\u4f86\u6162\u6162\u8ddf\u4f60\u89e3\u91cb\u3002\u300d<\/p>\n<h3>IEnumerable<\/h3>\n<p>\u525b\u525b<code>in<\/code>\u5f8c\u9762\u8981\u63a5\u7684\u8b8a\u6578\u662f\u7e7c\u627f<code>IEnumerable<\/code>\u4ecb\u9762\u7684\u7269\u4ef6\u7684\u8b8a\u6578\uff0c\u81f3\u65bc\u4ec0\u9ebc\u662f<code>IEnumerable<\/code>\u5c31\u8acb\u5927\u5bb6\u770b\u4e00\u4e0b<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/api\/system.collections.ienumerable?view=net-5.0\" title=\"msdn IEnumerable\" rel=\"nofollow noopener\" target=\"_blank\">msdn IEnumerable<\/a>\uff0c\u4e0d\u904e\u7c21\u55ae\u8b1b\u5c31\u662f\u4ed6\u6709\u5be6\u4f5c\u5de1\u8a2a\u529f\u80fd\u7684\u7269\u4ef6\u3002<\/p>\n<h2>\u8cc7\u6599\u7d50\u69cb data structure<\/h2>\n<p>\u597d\u4e86\uff0c\u4ecb\u7d39\u5b8c<code>foreach<\/code>\u8ddf<code>IEnumerable<\/code>\u4e4b\u5f8c\u76f8\u4fe1\u8b80\u8005\u5011\u61c9\u8a72\u8ddf\u5c0f\u5149\u4e00\u6a23\u6eff\u982d\u554f\u865f\uff0c\u5176\u5be6\u7c21\u55ae\u8aaa\u660e\u5c31\u662f\u5927\u5bb6\u9084\u4e0d\u8a8d\u8b58\u8cc7\u6599\u7d50\u69cb\u9019\u500b\u6771\u897f\uff0c\u6240\u4ee5\u95dc\u65bc\u8cc7\u6599\u7d50\u69cb\u662f\u4ec0\u9ebc\u8acb\u5927\u5bb6\u5148\u770b\u4e00\u4e0b<a href=\"https:\/\/zh.m.wikipedia.org\/wiki\/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84\" title=\"wiki \u8cc7\u6599\u7d50\u69cb\" rel=\"nofollow noopener\" target=\"_blank\">wiki \u8cc7\u6599\u7d50\u69cb<\/a>\u7684\u8aaa\u660e\uff0c\u7c21\u55ae\u8b1b\u5c31\u662f\u5b58\u653e\u8cc7\u6599\u7684\u4e00\u7a2e\u7d50\u69cb\uff0c\u7531\u65bc\u76ee\u524d\u5927\u591a\u6578\u8a9e\u8a00\u90fd\u6709\u5be6\u4f5c\u9019\u4e9b\u985e\u578b\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u6240\u4ee5\u8b80\u8005\u5011\u4e0d\u9700\u8981\u53bb\u5be6\u4f5c\uff0c\u4e0d\u904e\u9084\u662f\u8981\u53bb\u4e86\u89e3\u5404\u500b\u7d50\u69cb\u7684\u7279\u6027\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u4f86\u8aaa\u660e\u4e00\u4e0b<code>List<\/code>\u8ddf<code>Dictionary<\/code>\u9019\u5169\u7a2e\u8cc7\u6599\u7d50\u69cb\u3002<\/p>\n<h3>List<\/h3>\n<p>\u9996\u5148\u6211\u5011\u5148\u4f86\u770b\u770b<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/api\/system.collections.generic.list-1?view=net-5.0\" title=\"msdn List\" rel=\"nofollow noopener\" target=\"_blank\">msdn List<\/a>\u7684\u5167\u5bb9\uff0c\u7c21\u55ae\u7528\u6cd5\u4e0d\u5916\u4e4e\u662f\u65b0\u589e\u3001\u522a\u9664\u3001\u66f4\u65b0\u4ee5\u53ca\u5217\u51fa\u5bb9\u5668\u5167\u7684\u6578\u91cf\u9084\u6709\u5de1\u8a2a\u9019\u4e9b\u9700\u6c42\uff0c\u8207\u63a5\u4e0b\u4f86\u8981\u8aaa\u660e\u7684<code>Dictionary<\/code>\u5dee\u5225\u5728\u65bc\u5b83\u662f\u4e00\u500b\u9023\u7e8c\u7684\u8cc7\u6599\uff0c\u7576\u8981\u53d6\u5f97\u8a72\u8cc7\u6599\u6642\u9664\u975e\u77e5\u9053\u4ed6\u662f\u7b2c\u5e7e\u500b\u4f4d\u7f6e\u5426\u5247\u5c31\u8981\u4e00\u500b\u4e00\u500b\u5730\u5c0b\u627e\uff0c\u56e0\u6b64\u8a18\u61b6\u9ad4\u4f7f\u7528\u8f03Dictionary\u5c11\uff0c\u4f46\u662f\u5c0b\u627e\u7279\u5b9a\u5143\u7d20\u7684\u6548\u7387\u8f03\u4f4e\uff0c\u4e0b\u9762\u5217\u51fa\u5176\u65b0\u589e\u3001\u522a\u9664\u3001\u66f4\u65b0\u4ee5\u53ca\u5217\u51fa\u5bb9\u5668\u5167\u7684\u6578\u91cf\u9084\u6709\u5de1\u8a2a\u7684\u8a9e\u6cd5\u3002<\/p>\n<pre><code class=\"language-csharp\">\/\/ List\u7684\u521d\u59cb\u5316\nvar list = new List&lt;int&gt;(){1,2,3,4,5,6};\n\n\/\/ \u65b0\u589e\nlist.Add(3);\n\n\/\/ \u53d6\u51fa\u7b2c\u4e09\u500b\u4f4d\u7f6e\u7684\u8cc7\u6599\nvar item = list[3];\n\n\/\/ \u79fb\u9664\u525b\u525b\u53d6\u51fa\u7684\u8cc7\u6599\nlist.Remove(item);\n\n\/\/ \u522a\u9664\u7b2c0\u500b\u7684\u8cc7\u6599\nlist.RemoveAt(0);\n\n\/\/ \u5217\u51fa\u6578\u91cf\nConsole.WriteLine(list.Count);\n\n\/\/ \u5de1\u8a2a\nforeach(var ite in list)\n{\n    Console.WriteLine(ite);\n}<\/code><\/pre>\n<p>\u5beb\u5b8c\u4e4b\u5f8c\u767c\u73fe\u6709\u500b\u8981\u6ce8\u610f\u7684\u4e8b\u9805\uff0c\u5c31\u662fList\u7684\u4f4d\u7f6e\u7b2c\u4e00\u500b\u7684\u662f0\u4ee5\u6b64\u985e\u63a8\uff0c\u4f7f\u7528\u6642\u4e0d\u8981\u5f04\u932f\u56c9\u3002<\/p>\n<h3>Dictionary<\/h3>\n<p>\u4ecb\u7d39\u5b8c<code>List<\/code>\u5f8c<code>Dictionary<\/code>\u5c31\u6bd4\u8f03\u7c21\u55ae\u4e86\uff0c\u7c21\u55ae\u8aaa\u660e\u5c31\u662f\u8aaa\u591a\u4e86\u4e00\u500b\u65c5\u793e\u5927\u5ef3\u4f86\u544a\u8a34\u4f7f\u7528\u8005\u8b8a\u6578\u4f4f\u5728\u54ea\u88e1\uff0c\u800c\u4e0d\u7528\u81ea\u5df1\u53bb\u8a18\u8aaa\u8b8a\u6578\u4f4f\u5728\u54ea\u88e1\uff0c\u6240\u4ee5\u5728\u627e\u7684\u6642\u5019\u4e5f\u4e0d\u9700\u8981\u4e00\u9593\u623f\u9593\u4e00\u9593\u623f\u9593\u7684\u770b\uff0c\u6240\u4ee5\u627e\u7279\u5b9a\u8cc7\u6599\u7684\u901f\u5ea6\u5feb\u5f88\u591a\uff0c\u4e0d\u514d\u4fd7\u5730\u4e5f\u5c07\u8cc7\u6599\u7684\u8655\u7406\u5f0f\u5b50\u7d00\u9304\u5728\u4e0b\u9762\u3002<\/p>\n<pre><code class=\"language-csharp\">\/\/ Dictionary\u7684\u521d\u59cb\u5316\nvar dic = new Dictionary&lt;string, int&gt;()\n{\n    {&quot;1&quot;, 1},\n    {&quot;2&quot;, 2},\n    {&quot;3&quot;, 3},\n    {&quot;4&quot;, 4},\n    {&quot;5&quot;, 5},\n    {&quot;6&quot;, 6}\n};\n\n\/\/ \u65b0\u589e\ndic.Add(&quot;7&quot;, 7);\n\n\/\/ \u53d6\u503c\nvar item = dic[&quot;3&quot;];\n\n\/\/ \u522a\u9664\u503c\ndic.Remove(&quot;3&quot;);\n\n\/\/ \u5217\u51fa\u500b\u6578\nConsole.WriteLine(dic.Count);\n\n\/\/ \u5de1\u8a2a\nforeach(var ite in dic)\n{\n    Console.WriteLine(ite.Key);\n    Console.WriteLine(ite.Value);\n    Console.WriteLine(ite);\n}<\/code><\/pre>\n<p>\u6700\u5f8c\u8981\u8a18\u4f4f\u56e0\u70ba\u662f\u4e00\u4eba\u4e00\u9593\u623f\uff0c\u6240\u4ee5\u4e0d\u80fd\u5169\u500b\u8b8a\u6578\u653e\u5728\u540c\u4e00\u500b<code>Key<\/code>\u88e1\u9762\u5594\u3002<\/p>\n<h2>\u5f8c\u8a18<\/h2>\n<p>\u300c\u524d\u8f29\u600e\u9ebc\u4eca\u5929\u8b1b\u5b8c\u5f8c\u6211\u6574\u500b\u982d\u660f\u8166\u8139\u7684\uff1f\u300d<br \/>\n\u7576\u5927\u982d\u8aaa\u5b8c\u5f8c\uff0c\u5c0f\u5149\u6574\u500b\u5448\u73fe\u660f\u8ff7\u7684\u72c0\u6cc1\uff0c\u9019\u6642\u5927\u982d\u4e00\u908a\u54c8\u54c8\u5927\u7b11\u4e00\u908a\u5c0d\u4ed6\u9019\u9ebc\u8aaa\u3002<br \/>\n\u300c\u5f8c\u982d\u660f\u773c\u82b1\u4ee3\u8868\u4f60\u6709\u8a8d\u771f\u807d\uff0c\u4f46\u662f\u9084\u4e0d\u80fd\u5438\u6536\uff0c\u6211\u5efa\u8b70\u4f60\u53bb\u770b\u770b\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u66f8\uff0c\u76f8\u4fe1\u5c0d\u4f60\u6709\u6240\u5e6b\u52a9\u3002\u300d<br \/>\n\u807d\u5b8c\u4e4b\u5f8c\u5c0f\u5149\u4f3c\u61c2\u975e\u61c2\u7684\u9ede\u9ede\u982d\uff0c\u4e0d\u904e\u904e\u5e7e\u79d2\u5f8c\u53c8\u9019\u9ebc\u554f\u5230\u3002<br \/>\n\u300c\u90a3\u4ec0\u9ebc\u662f\u7269\u4ef6\u8ddf\u4ecb\u9762\uff1f\u300d<br \/>\n\u807d\u5230\u9019\u76f8\u4fe1\u8b80\u8005\u61c9\u8a72\u6703\u6709\u540c\u6a23\u7684\u554f\u984c\uff0c\u4e0d\u904e\u5f85\u5f8c\u7e8c\u6703\u5728\u8ddf\u5927\u5bb6\u4ecb\u7d39\uff0c\u656c\u8acb\u671f\u5f85\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9084\u6709\u4e00\u7a2e\u8ff4\u5708 \u6628\u5929\u4ecb\u7d39\u6642\u5927\u982d\u9084\u6709\u4e00\u500b\u8ff4\u5708\u7684\u65b9\u5f0f\u6c92\u6709\u4ecb\u7d39\uff0c\u70ba\u4ec0\u9ebc\u6628\u5929\u4e0d\u4ecb\u7d39\u5462\uff0c\u662f\u56e0\u70ba\u7bc7\u5e45\u95dc\u4fc2\u9084\u662f\u6709\u5176\u4ed6\u539f\u56e0\uff0c\u8b93\u6211 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2021\/09\/21\/data-structure\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008D-24 \u8cc7\u6599\u7d50\u69cb data structure ? List ? Dictionary\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[90,71,92],"class_list":["post-2064","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2021ironman","tag-c","tag-dotnetcore"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=2064"}],"version-history":[{"count":22,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2064\/revisions"}],"predecessor-version":[{"id":2944,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2064\/revisions\/2944"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=2064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=2064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=2064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}