Answer the question
In order to leave comments, you need to log in
Traversing a two-dimensional array with a single foreach in C#?
Subject:
<font color="black"><font color="#0000ff">using</font> System;<br/> <font color="#0000ff">using</font> System.Collections.<font color="#2B91AF">Generic</font>;<br/> <font color="#0000ff">using</font> System.Linq;<br/> <br/> <font color="#0000ff">namespace</font> Test<br/> {<br/> <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">class</font> ExMethods<br/> {<br/> <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#2B91AF">IEnumerable</font><Tuple<<font color="#0000ff">int</font>, <font color="#0000ff">int</font>>> Create2DIterator(<font color="#0000ff">this</font> <font color="#0000ff">int</font>[,] arr2D)<br/> {<br/> <font color="#0000ff">for</font> (<font color="#0000ff">int</font> i = 0, j; i < arr2D.GetLength(0); ++i)<br/> <font color="#0000ff">for</font> (j = 0; j < arr2D.GetLength(1); ++j)<br/> <font color="#0000ff">yield</font> <font color="#0000ff">return</font> Tuple.Create(i, j);<br/> }<br/> }<br/> <br/> <font color="#0000ff">class</font> Program<br/> {<br/> <font color="#0000ff">static</font> <font color="#0000ff">void</font> Main()<br/> {<br/> <font color="#0000ff">int</font>[,] arr2D = {<br/> {11, 12, 13},<br/> {21, 22, 23},<br/> {31, 32, 33},<br/> {41, 42, 43},<br/> {51, 52, 53}<br/> };<br/> <br/> <font color="#0000ff">foreach</font> (<font color="#0000ff">var</font> t <font color="#0000ff">in</font> arr2D.Create2DIterator())<br/> {<br/> <font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">"Row: {0} Col: {1} Val: {2}"</font>, t.Item1, t.Item2, arr2D[t.Item1, t.Item2]);<br/> }<br/> <br/> <font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">"----------"</font>);<br/> <br/> <font color="#0000ff">foreach</font> (<font color="#0000ff">var</font> t <font color="#0000ff">in</font> <font color="#0000ff">new</font>[] {<font color="#0000ff">new</font> {Row = 0, Col = 0}, <font color="#0000ff">new</font> {Row = 0, Col = 1}, <font color="#0000ff">new</font> {Row = 0, Col = 2},<br/> <font color="#0000ff">new</font> {Row = 1, Col = 0}, <font color="#0000ff">new</font> {Row = 1, Col = 1}, <font color="#0000ff">new</font> {Row = 1, Col = 2},<br/> <font color="#0000ff">new</font> {Row = 2, Col = 0}, <font color="#0000ff">new</font> {Row = 2, Col = 1}, <font color="#0000ff">new</font> {Row = 2, Col = 2},<br/> <font color="#0000ff">new</font> {Row = 3, Col = 0}, <font color="#0000ff">new</font> {Row = 3, Col = 1}, <font color="#0000ff">new</font> {Row = 3, Col = 2},<br/> <font color="#0000ff">new</font> {Row = 4, Col = 0}, <font color="#0000ff">new</font> {Row = 4, Col = 1}, <font color="#0000ff">new</font> {Row = 4, Col = 2}})<br/> {<br/> <font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">"Row: {0} Col: {1} Val: {2}"</font>, t.Row, t.Col, arr2D[t.Row, t.Col]);<br/> }<br/> <br/> <font color="#2B91AF">Console</font>.ReadKey();<br/> }<br/> }<br/> }</font><br/> <br/> <font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
<font color="black"><font color="#0000ff">new</font>[] {<font color="#0000ff">new</font> {Row = 0, Col = 0}, <font color="#0000ff">new</font> {Row = 0, Col = 1}, <font color="#0000ff">new</font> {Row = 0, Col = 2},<br/> <font color="#0000ff">new</font> {Row = 1, Col = 0}, <font color="#0000ff">new</font> {Row = 1, Col = 1}, <font color="#0000ff">new</font> {Row = 1, Col = 2},<br/> <font color="#0000ff">new</font> {Row = 2, Col = 0}, <font color="#0000ff">new</font> {Row = 2, Col = 1}, <font color="#0000ff">new</font> {Row = 2, Col = 2},<br/> <font color="#0000ff">new</font> {Row = 3, Col = 0}, <font color="#0000ff">new</font> {Row = 3, Col = 1}, <font color="#0000ff">new</font> {Row = 3, Col = 2},<br/> <font color="#0000ff">new</font> {Row = 4, Col = 0}, <font color="#0000ff">new</font> {Row = 4, Col = 1}, <font color="#0000ff">new</font> {Row = 4, Col = 2}}</font><br/> <br/> <font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
Answer the question
In order to leave comments, you need to log in
For example like this:
var array = from i in Enumerable.Range(0, 5)
from j in Enumerable.Range(0, 3)
select new { Row = i, Col = j };
Something likeEnumerable.Range(0, 15).Select(i => new { Row = i / 3, Col = i % 3})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question