1

Something like:

forelement (element G_Element, Grid)
{
    Grid[G_Element.dim1, G_Element.dim2] = 
        new clsGridElement(G_Element.dim1, G_Element.dim2);
}

instead of

for (int X = 0; X < GridWidth; X++) 
    for (int Y = 0; Y < GridHeight; Y++) 
        Grid[X, Y] = new clsGridElement(X, Y);

If something doesn't innately exist, is that something that could be created?

Thanks, Tim

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Mythics
  • 773
  • 1
  • 10
  • 19
  • 3
    [foreach](http://msdn.microsoft.com/en-us/library/ttw7t8t6%28v=vs.80%29.aspx); [IEnumerable](http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx) and [yield](http://msdn.microsoft.com/en-us/library/9k7k7cf0%28v=vs.80%29.aspx) if you're writing your own custom iterators. In your example, the `for` loop seems simpler, though. – Robert Harvey Nov 01 '12 at 17:08
  • Check out results of "C# linq Cartesian product" search to construct suitable enumeration. There are couple good posts by the Eric Lippert here like http://stackoverflow.com/questions/4073713/is-there-a-good-linq-way-to-do-a-cartesian-product and http://stackoverflow.com/questions/4073713/is-there-a-good-linq-way-to-do-a-cartesian-product and – Alexei Levenkov Nov 01 '12 at 17:20

3 Answers3

6

You could do this - just make a custom type that exposes these, and use a regular foreach:

public class TwoDimensionalIterator
{
    public TwoDimensionalIterator(int i, int j)
    {
       this.Dim1 = i; this.Dim2 = j;
    }
    public int Dim1 { get; private set; }
    public int Dim2 { get; private set; }
}

Then make an extension method somewhere to return an enumerable of this:

public static IEnumerable<TwoDimensionalIterator> IterateTwoDim<T>(this T[,] array)
{
     for (int i=0;i<array.GetLength(0);++i)
         for (int j=0;i<array.GetLength(1);++j)
            yield return new TwoDimensionalIterator(i,j);
}

With this, you could then do:

foreach(var index in Grid.IterateTwoDim())
{
    Grid[index.Dim1, index.Dim2] = new clsGridElement(index.Dim1, index.Dim2);
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

Not sure exactly what you are trying to do here, or why, or what you expect to get from it, but if you implement your own iterator than implements the IEnumerator interface then you could create something that would hit every cell in your 2D (or more) collection.

Of course, you won't actually gain anything performance-wise from doing this versus just using nested loops, but I guess it'd be syntactic sugar.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

Such creation of indexes can be obtained by using "Cartesian product" of all indexes. Here is sample based on Is there a good LINQ way to do a cartesian product? (courtesy of the Eric Lippert):

var allIndexes = from dim1 in Enumerable.Range(0, Grid.GetLength(0))
from dim2 in Enumerable.Range(0, Grid.GetLength(1))
select new {dim1, dim2};

foreach (var G_Element in allIndexes)
{
    Grid[G_Element.dim1, G_Element.dim2] = 
        new clsGridElement(G_Element.dim1, G_Element.dim2);
}
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179