Обход двумерного массива при помощи одного foreach на C#?

Сабж:
<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/>
&nbsp;&nbsp;<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">class</font> ExMethods<br/>
&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#2B91AF">IEnumerable</font>&lt;Tuple&lt;<font color="#0000ff">int</font>, <font color="#0000ff">int</font>&gt;&gt; Create2DIterator(<font color="#0000ff">this</font> <font color="#0000ff">int</font>[,] arr2D)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">for</font> (<font color="#0000ff">int</font> i = 0, j; i &lt; arr2D.GetLength(0); ++i)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">for</font> (j = 0; j &lt; arr2D.GetLength(1); ++j)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">yield</font> <font color="#0000ff">return</font> Tuple.Create(i, j);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;<font color="#0000ff">class</font> Program<br/>
&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">static</font> <font color="#0000ff">void</font> Main()<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">int</font>[,] arr2D = {<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {11, 12, 13},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {21, 22, 23},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {31, 32, 33},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {41, 42, 43},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {51, 52, 53}<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">foreach</font> (<font color="#0000ff">var</font> t <font color="#0000ff">in</font> arr2D.Create2DIterator())<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">&quot;Row: {0} Col: {1} Val: {2}&quot;</font>, t.Item1, t.Item2, arr2D[t.Item1, t.Item2]);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">&quot;----------&quot;</font>);<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">&quot;Row: {0} Col: {1} Val: {2}&quot;</font>, t.Row, t.Col, arr2D[t.Row, t.Col]);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2B91AF">Console</font>.ReadKey();<br/>
&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
&nbsp;&nbsp;}<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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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>


используя LINQ, в одну строчку.
  • Вопрос задан
  • 7066 просмотров
Решения вопроса 1
clx
@clx
Например так:
var array = from i in Enumerable.Range(0, 5)
            from j in Enumerable.Range(0, 3)
            select new { Row = i, Col = j };
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
ostapbender
@ostapbender
Что-то типа Enumerable.Range(0, 15).Select(i => new { Row = i / 3, Col = i % 3})
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
23 апр. 2024, в 19:05
15000 руб./за проект
23 апр. 2024, в 19:01
7000 руб./за проект
23 апр. 2024, в 18:47
10000 руб./за проект