Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you have won't work as the <code>new</code> occurs before the call to <a href="http://msdn.microsoft.com/en-us/library/bb348899.aspx" rel="nofollow noreferrer"><code>Repeat</code></a>. You need something that also repeats the creation of the array. This can be achieved using the <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx" rel="nofollow noreferrer"><code>Enumerable.Range</code> method</a> to generate a range and then performing a <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.select.aspx" rel="nofollow noreferrer"><code>Select</code> operation</a> that maps each element of the range to a new array instance (as in <a href="https://stackoverflow.com/questions/1099731/initialize-a-jagged-array-the-linq-way/1099794#1099794">David B's answer</a>).</p> <p>However, I think that you are trying to use LINQ where it isn't really appropriate to do so in this case. What you had prior to the LINQ solution is just fine. Of course, if you wanted a LINQ-style approach similar to <a href="http://msdn.microsoft.com/en-us/library/bb348899.aspx" rel="nofollow noreferrer"><code>Enumerable.Repeat</code></a>, you could write your own extension method that generates a new item, such as:</p> <pre><code> public static IEnumerable&lt;TResult&gt; Repeat&lt;TResult&gt;( Func&lt;TResult&gt; generator, int count) { for (int i = 0; i &lt; count; i++) { yield return generator(); } } </code></pre> <p>Then you can call it as follows:</p> <pre><code> var result = Repeat(()=&gt;new double[rowCount], columnCount).ToArray(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload