{"id":3619,"date":"2012-08-07T08:34:41","date_gmt":"2012-08-06T14:34:41","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/3619"},"modified":"2012-08-07T09:47:17","modified_gmt":"2012-08-07T00:47:17","slug":"c-ienumerable-%e3%81%a8-ienumerator-%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e7%8b%ac%e8%87%aa%e3%81%ae%e3%83%aa%e3%82%b9%e3%83%88%e3%82%92%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/3619","title":{"rendered":"[C#] IEnumerable<> \u3068 IEnumerator<> \u3092\u4f7f\u3063\u3066\u72ec\u81ea\u306e\u30ea\u30b9\u30c8\u3092\u4f5c\u3063\u3066\u307f\u308b"},"content":{"rendered":"<p>HtmlDom \u306e\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3092\u4f5c\u308b\u524d\u306b\u3001List&lt;&gt; \u3067\u5229\u7528\u3055\u308c\u308b LINQ \u304c\u3069\u3046\u52d5\u304f\u306e\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3002<br \/>\n\u624b\u59cb\u3081\u306b\u306f\u3001IEnumerable \u3068 IEnumerator \u3092\u4f7f\u3048\u3070\u3088\u3044\u3068\u306e\u3053\u3068\u306a\u306e\u3067\u3001\u81ea\u524d\u3067\u4f5c\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>IEnumerable(T) \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9 (System.Collections.Generic)<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/library\/9eekhta0.aspx\">http:\/\/msdn.microsoft.com\/ja-jp\/library\/9eekhta0.aspx<\/a><br \/>\nIEnumerable.GetEnumerator \u30e1\u30bd\u30c3\u30c9 (System.Collections)<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/library\/system.collections.ienumerable.getenumerator.aspx\">http:\/\/msdn.microsoft.com\/ja-jp\/library\/system.collections.ienumerable.getenumerator.aspx<\/a><\/p>\n<p>MSDN \u81ea\u4f53\u306b\u65e2\u306b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u304c\u8f09\u3063\u3066\u3044\u308b\u306e\u3067\u305d\u308c\u3092\u4f7f\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u25a0\u30e9\u30f3\u30c0\u30e0\u306a\u5024\u3092\u8fd4\u3059 RandomNum \u30ea\u30b9\u30c8\u3092\u4f5c\u308b<\/p>\n<p>\u30e9\u30f3\u30c0\u30e0\u306a\u5024\u3092\u8fd4\u3059\u306a\u3089\u3070\u3001Random \u3092\u4f7f\u3048\u3070\u3044\u3044\u3060\u3051\u306a\u306e\u3067\u3059\u304c\u3001\u307e\u3041\u3001\u3053\u308c\u3092 LINQ \u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nforeach ( var i in rnd ) {\r\n  Debug.Print( i );\r\n}\r\n<\/pre>\n<p>\u306a\u3093\u3066\u3053\u3068\u3092\u3084\u308b\u3068\u7121\u9650\u306b\u7e70\u308a\u8fd4\u3057\u3066\u3057\u307e\u3046\u3088\u3046\u306a\u30e2\u30ce\u3067\u3059\u306d\uff08\u82e6\u7b11\uff09\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\tclass RandomNum : IEnumerable&lt;int&gt;\r\n\t{\r\n\t\tint _max ;\r\n\t\tpublic RandomNum(int max)\r\n\t\t{\r\n\t\t\t_max = max;\r\n\t\t}\r\n\r\n\t\tpublic IEnumerator&lt;int&gt; GetEnumerator()\r\n\t\t{\r\n\t\t\treturn new RandomNumEnum(_max);\r\n\t\t}\r\n\r\n\t\tSystem.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()\r\n\t\t{\r\n\t\t\treturn new RandomNumEnum(_max);\r\n\t\t}\r\n\t}\r\n\tclass RandomNumEnum : IEnumerator&lt;int&gt;\r\n\t{\r\n\t\tint _max;\r\n\t\tRandom _rnd;\r\n\t\tint _cur;\r\n\t\tpublic RandomNumEnum(int max)\r\n\t\t{\r\n\t\t\t_max = max;\r\n\t\t\t_rnd = new Random();\r\n\t\t}\r\n\t\tpublic int Current\r\n\t\t{\r\n\t\t\tget { return _cur; }\r\n\t\t}\r\n\r\n\t\tpublic void Dispose()\r\n\t\t{\r\n\t\t}\r\n\r\n\t\tobject System.Collections.IEnumerator.Current\r\n\t\t{\r\n\t\t\tget { return _cur; }\r\n\t\t}\r\n\r\n\t\tpublic bool MoveNext()\r\n\t\t{\r\n\t\t\t_cur = _rnd.Next(_max);\r\n\t\t\treturn true;\r\n\t\t}\r\n\r\n\t\tpublic void Reset()\r\n\t\t{\r\n\t\t\t_rnd = new Random();\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>RandomNum \u304c\u3001List&lt;int&gt; \u306b\u3042\u305f\u308b\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3067\u3001RandomNumEnum \u304c\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3067\u3059\u3002\u3061\u3087\u3046\u3069 C++ \u306e list&lt;int&gt; \u3068 list&lt;int&gt;::iterator \u306e\u95a2\u4fc2&#8230;\u3067\u3044\u3044\u306e\u304b\u306a\uff1f IEnumerable, IEnumerator \u3067\u5ba3\u8a00\u3055\u308c\u3066\u3044\u308b\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u53f3\u30af\u30ea\u30c3\u30af\u300c\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u5b9f\u88c5\u300d\u2192\u300c\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306e\u5b9f\u88c5\u300d\u3092\u9078\u629e\u3059\u308b\u3068 Visual Studio \u304c\u3072\u306a\u5f62\u3092\u4f5c\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u306f\u3001<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate void button1_Click(object sender, EventArgs e)\r\n{\r\n\t\/\/ \u521d\u671f\u5316\r\n\tRandomNum rnd = new RandomNum(100);\r\n\t\/\/ \u6700\u521d\u306e50\u4ef6\u3092\u53d6\u5f97\r\n\tlistBox1.DataSource = rnd.Take(50).ToList();\r\n}\r\n\r\nprivate void button2_Click(object sender, EventArgs e)\r\n{\r\n\t\/\/ \u521d\u671f\u5316\r\n\tRandomNum rnd = new RandomNum(100);\r\n\t\/\/ 10\u304b\u308920\u307e\u3067\u306e\u5024\u3092\u53d6\u5f97\r\n\tlistBox1.DataSource = rnd.Where(x =&gt; 10 &lt;= x &amp;&amp; x &lt;= 20)\r\n\t\t.Take(30).ToList();\r\n}\r\n<\/pre>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306b\u3001Take \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u6700\u521d\u306e50\u4ef6\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002foreach \u3092\u4f7f\u3046\u3068\u7121\u9650\u306b\u53d6\u5f97\u3067\u304d\u308b\u306e\u3067\u66b4\u8d70\u3059\u308b\u30cf\u30ba\u3067\u3059\u3002<br \/>\n\u307e\u305f\u3001where \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u6761\u4ef6\u3092\u4ed8\u3051\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u3042\u307e\u308a\u610f\u5473\u304c\u3042\u308a\u307e\u305b\u3093\u304c\u3002<\/p>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/08\/wpid-dworkblogimage20120807_01org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/08\/wpid-dworkblogimage20120807_01thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>\u666e\u901a\u306b\u30e9\u30f3\u30c0\u30e0\u306b\u8868\u793a\u3057\u305f\u306e\u3068\u5909\u308f\u308a\u307e\u305b\u3093\u304c\u3001\u4e00\u6c17\u306b\u30e9\u30f3\u30c0\u30e0\u306a\u5024\u3092\u4f5c\u308b\u6642\u306b\u4fbf\u5229\u304b\u3068\uff08\u4fbf\u5229\u306a\u306e\u304b\uff1f\uff09<\/p>\n<p>\u25a0\u30b5\u30a4\u30af\u30ea\u30c3\u30af\u306a\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u4f5c\u308b<\/p>\n<p>\u306a\u3093\u3068\u306a\u304f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u4f7f\u3044\u65b9\u304c\u5206\u304b\u3063\u305f\u306e\u3067\u3001\u4eca\u5ea6\u306f\u30b5\u30a4\u30af\u30ea\u30c3\u30af\u306a\u914d\u5217\u3092\u4f5c\u3063\u3066\u307f\u307e\u3059\u3002\u6700\u5f8c\u306e\u8981\u7d20\u306e\u6b21\u304c\u5148\u982d\u306b\u306a\u3063\u3066\u3050\u308b\u3050\u308b\u3068\u307e\u308f\u308b\u3060\u3051\u306e\u30ea\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<p>\u30e9\u30f3\u30c0\u30e0\u5024\u3068\u540c\u3058\u3088\u3046\u306b\u3001IEnumerable&lt;string&gt; \u3092\u5b9f\u88c5\u3059\u308b\u30ea\u30b9\u30c8\u3068\u3001IEnumerator&lt;string&gt; \u3092\u5b9f\u88c5\u3059\u308b\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\tclass CyclicArray : IEnumerable&lt;string&gt;\r\n\t{\r\n\t\tList&lt;string&gt; _lst;\r\n\t\tpublic CyclicArray(List&lt;string&gt; lst)\r\n\t\t{\r\n\t\t\t_lst = lst;\r\n\t\t}\r\n\r\n\t\tpublic IEnumerator&lt;string&gt; GetEnumerator()\r\n\t\t{\r\n\t\t\treturn new CyclicArrayEnum(_lst);\r\n\t\t}\r\n\r\n\t\tSystem.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()\r\n\t\t{\r\n\t\t\treturn new CyclicArrayEnum(_lst);\r\n\t\t}\r\n\r\n\t}\r\n\tclass CyclicArrayEnum : IEnumerator&lt;string&gt;\r\n\t{\r\n\t\tList&lt;string&gt; _lst;\r\n\t\tint _pos;\r\n\t\tpublic CyclicArrayEnum(List&lt;string&gt; lst)\r\n\t\t{\r\n\t\t\t_lst = lst;\r\n\t\t\t_pos = -1;\r\n\t\t}\r\n\r\n\t\tpublic string Current\r\n\t\t{\r\n\t\t\tget { return _lst&#x5B;_pos] ; }\r\n\t\t}\r\n\r\n\t\tpublic void Dispose()\r\n\t\t{\r\n\t\t}\r\n\r\n\t\tobject System.Collections.IEnumerator.Current\r\n\t\t{\r\n\t\t\tget { return _lst&#x5B;_pos]; }\r\n\t\t}\r\n\r\n\t\tpublic bool MoveNext()\r\n\t\t{\r\n\t\t\t_pos++;\r\n\t\t\tif (_pos &gt;= _lst.Count)\r\n\t\t\t\t_pos = 0;\r\n\t\t\treturn true;\r\n\t\t\tthrow new NotImplementedException();\r\n\t\t}\r\n\r\n\t\tpublic void Reset()\r\n\t\t{\r\n\t\t\t_pos = -1;\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>\u30b5\u30a4\u30af\u30ea\u30c3\u30af\u306a\u306e\u3067\u3001CyclicArrayEnum::MoveNext \u30e1\u30bd\u30c3\u30c9\u306f\u5fc5\u305a true \u3092\u8fd4\u3057\u307e\u3059\u3002\u3053\u308c\u3082 foreach \u3092\u3059\u308b\u3068\u7121\u9650\u5927\u306e\u30d1\u30bf\u30fc\u30f3\u306b\u306a\u308b\u306e\u3067\u3059\u304c\u3001\u307e\u3042\u826f\u3057\u3068\u3057\u307e\u3059\u3002\u5185\u90e8\u7684\u306b\u306f _pos \u3067\u73fe\u5728\u4f4d\u7f6e\u3092\u4fdd\u5b58\u3057\u3066\u304a\u3044\u3066\u3001\u672b\u5c3e\u306b\u6765\u308b\u3068\u5148\u982d\u306b\u623b\u3059\u3068\u3044\u3046\u51e6\u7406\u3092\u5165\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate void button3_Click(object sender, EventArgs e)\r\n{\r\n\tvar lst = new List&lt;string&gt;() {\r\n\t\t&amp;quot;1:masuda&amp;quot;,\r\n\t\t&amp;quot;2:tomoaki&amp;quot;,\r\n\t\t&amp;quot;3:yamasaki&amp;quot;,\r\n\t\t&amp;quot;4:yumi&amp;quot;,\r\n\t\t&amp;quot;5:kaho&amp;quot;,\r\n\t};\r\n\t\/\/ \u521d\u671f\u5316\r\n\tvar ca = new CyclicArray(lst);\r\n\t\/\/ 13\u4ef6\u53d6\u5f97\u3059\u308b\r\n\tlistBox1.DataSource =\r\n\t\tca.Take(13).ToList();\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/08\/wpid-dworkblogimage20120807_02org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/08\/wpid-dworkblogimage20120807_02thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u3046\u5834\u5408\u306f\u3001\u3053\u3093\u306a\u98a8\u306b List&lt;string&gt; \u3092\u6e21\u3057\u3066\u3001Take \u30e1\u30bd\u30c3\u30c9\u3067\u4e00\u5b9a\u6570\u3092\u53d6\u308b\u3068\u3044\u3046\u30b9\u30bf\u30a4\u30eb\u3067\u3059\u306d\u3002<br \/>\n\u914d\u5217\u3068\u3057\u3066\u306f5\u3064\u306e\u8981\u7d20\u3057\u304b\u306a\u3044\u306e\u3067\u3059\u304c\u3001take \u3067 13\u500b\u3092\u8981\u6c42\u3059\u308b\u3068\u30b5\u30a4\u30af\u30ea\u30c3\u30af\u306b\u8fd4\u3057\u3066\u304f\u308c\u307e\u3059\u3002<br \/>\n\u7c21\u5358\u3067\u3059\u304c\u3001\u3053\u308c\u3067\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3068\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u306f\u4f5c\u308c\u305d\u3046\u306a\u306e\u304c\u5206\u304b\u308a\u307e\u3057\u305f\u3001\u3068\u3002<\/p>\n<p>\u25a0\u30c4\u30ea\u30fc\u69cb\u9020\u3092\u5b50\u5b6b\u307e\u3067\u5de1\u3063\u3066\u63a2\u3059\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u306f\u3069\u306e\u3088\u3046\u306b\u4f5c\u308b\u306e\u304b\uff1f<\/p>\n<p>\u666e\u901a\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u5834\u5408\u306f\u4e00\u6bb5\u968e\u3057\u304b\u8981\u7d20\u3092\u63a2\u3057\u306b\u884c\u304b\u306a\u3044\u306e\u3067\u3059\u304c\u3001XML \u69cb\u9020\u306e\u3088\u3046\u306b\u30c4\u30ea\u30fc\u69cb\u9020\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u518d\u5e30\u7684\u306b\u5b50\u8981\u7d20\u3092\u63a2\u3059\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3059\u3002<\/p>\n<p>\u306a\u306e\u3067\u3001\u3053\u308c\u3092\u64ec\u4f3c\u7684\u306b\u66f8\u3044\u3066\u307f\u308b\u3068&#8230;\u306a\u3068\u3053\u308d\u3067\u8a70\u307e\u3063\u3066\u3057\u307e\u3063\u305f\u306e\u3067\u5f8c\u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HtmlDom \u306e\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3092\u4f5c\u308b\u524d\u306b\u3001List&lt;&gt; \u3067\u5229\u7528\u3055\u308c\u308b LINQ \u304c\u3069\u3046\u52d5\u304f\u306e\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3002 \u624b\u59cb\u3081\u306b\u306f\u3001IEnumerable \u3068 IEnumerator \u3092\u4f7f\u3048\u3070\u3088\u3044\u3068\u306e\u3053\u3068\u306a\u306e\u3067\u3001\u81ea\u524d\u3067\u4f5c &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/3619\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-3619","post","type-post","status-publish","format-standard","hentry","category-csharp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=3619"}],"version-history":[{"count":4,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3619\/revisions"}],"predecessor-version":[{"id":3625,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3619\/revisions\/3625"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=3619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=3619"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=3619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}