0

Here are two examples :

I have an array with 3 items : Universe, planet and Continent I want to use a foreach loop like this :

  foreach(var universe in Universe)
  {
       foreach(var planet in Planet)
       {
             foreach(var continent in Continent)
             { 
                 // here comes my code
              } } }

In this case the array contains 3 elements and therefore I need 3 foreach statements.

Now lets say that I add 2 extra items to the array (Country and State) There would have to be 5 foreach loops.

But what if the array contains 600 items : then I would need to write 600 foreach statements...

So how can this be programatically solved?

  • Dynamic code addition?
  • Recursive codes (I have no experience with them)
  • ...
  • If `Universe`, `Earth` and `Continent` are collections why are they singular terms? Why would an Earth have multiple earths? And a continent can't really have multiple continents: it defies the definition of continent a bit. – Grant Thomas Aug 20 '13 at 10:58
  • ok - I just chose that as an example, but lets say planet? –  Aug 20 '13 at 10:59
  • It's still singular. Are each of these things collections, or what? Please give a concrete example and explanation. – Grant Thomas Aug 20 '13 at 10:59
  • Your question is not clear, you may want to explain what are you trying to achieve, Array is an object that contains collection of objects, for example A basket is an array, it may contain 10 apples, 6 oranges, and 4 banana. – Jegan Aug 20 '13 at 11:05
  • 2
    This question looks strangely similar to this one: http://stackoverflow.com/questions/18327942/dynamically-append-foreach-statements-to-c-sharp-code ... and again, you ask for a solution for Y when you should ask for a solution for X instead ([XYProblem](http://mywiki.wooledge.org/XyProblem)). – Corak Aug 20 '13 at 11:07
  • 1
    by the question you posted I think you are probably searching for a solution for some very simple problem that was made incredible complex by bad design decision and/or low understanding of the language – Fabio Marcolini Aug 20 '13 at 11:26
  • Maybe http://stackoverflow.com/a/10629938/1336590 will help. – Corak Aug 20 '13 at 11:46
  • So you want 600 variables? You know that if each collection is of 2 elements, the total number of cycles is 2^600? It's so much big that I don't even know how much big it's! – xanatos Aug 20 '13 at 12:16
  • @xanatos - If it is like in the other question (http://stackoverflow.com/questions/18327942/dynamically-append-foreach-statements-to-c-sharp-code) it's even 600! – Corak Aug 20 '13 at 12:20
  • Eric Lippert got the answer in his blog: http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx Your code should be CarthesianProduct(new [] { Universe, Planet, Continent}).Select(x => string.Join(",", x)).ToList() – adrianm Aug 20 '13 at 12:32

1 Answers1

1

If you have one array with three items in it, then in order to enumerate through the items you should only need one foreach loop.

List<Object> items = new List<Object> { Universe, Earth, Continent };
foreach (var item in items) { ... }

You would only need to write additional nested foreach loops if the objects contained by the outer array each themselves implemented IEnumerable, and you needed to enumerate these as well. Without seeing the problem that you are trying to solve, it is hard to make a recommendation. But I think that the assumption that you need to have nested foreach structures is incorrect.

Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174