{"id":42,"date":"2006-03-29T10:59:49","date_gmt":"2006-03-29T15:59:49","guid":{"rendered":"http:\/\/stein.everybody.org\/brain\/c-foreach-two-at-a-time\/"},"modified":"2011-03-10T10:57:33","modified_gmt":"2011-03-10T15:57:33","slug":"c-foreach-two-at-a-time","status":"publish","type":"post","link":"https:\/\/jeremystein.com\/brain\/c-foreach-two-at-a-time\/","title":{"rendered":"C# foreach two at a time"},"content":{"rendered":"<p>Do you want to iterate through a collection with a foreach, but retrieve two items at a time?<\/p>\n<p>You can&#8217;t do it.  The <a href=\"http:\/\/msdn.microsoft.com\/vcsharp\/programming\/language\/default.aspx\">spec<\/a> states specifically that the enumerator is inaccessible.<\/p>\n<p>I supposed you could do something like this:<\/p>\n<p><code><\/p>\n<pre>\nint[] a = {1, 2, 3, 4, 5, 6};\n\nint previousVal = -1;\nbool odd = false;\nforeach (int currentVal in a)\n{\n  odd = ! odd;\n  if (odd)\n  {\n    previousVal = currentVal;\n    continue;\n  }\n  Console.WriteLine(String.Format(\"{0}, {1}\",\n    previousVal, currentVal));\n}\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to iterate through a collection with a foreach, but retrieve two items at a time? You can&#8217;t do it. The spec states specifically that the enumerator is inaccessible. I supposed you could do something like this: int[] a = {1, 2, 3, 4, 5, 6}; int previousVal = -1; bool odd = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":0,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}