title: Dynamic nested loops to generate all possible combinations of array elements in c#
body: <p>I've searched stackoverflow and the web for similar questions, but i couldn't found the solution that i need.</p>
  
  <p>I'm programming a code-list-generator.</p>
  
  <p>so for example i have a List of chars like <code>List&lt;char&gt; { 'a', 'b', 'c' };</code>.</p>
  
  <p>and i have a couple of settings like <code>(int)minLength</code> of <code>2</code> and a <code>(int)maxLength</code> of <code>3</code>.</p>
  
  <p>and i want this output :</p>
  
  <pre><code>aa
  ab
  ac
  ba
  bb
  bc
  ca
  cb
  cc
  
  aaa
  aab
  aac
  aba
  abb
  abc
  aca
  acb
  acc
  
  baa
  bab
  bac
  bba
  bbb
  bbc
  bca
  bcb
  bcc
  
  caa
  cab
  cac
  cba
  cbb
  cbc
  cca
  ccb
  ccc
  </code></pre>
  
  <p>in gereral i would just kreate multidimensional loops, but i have to do this dynamically because of the diferent minLength, maxLength &amp; charList values.</p>
  
  <p>so i was going with a "self-calling-function" like this example:</p>
  
  <pre><code>    private void loop() {
          for( int i = 0; i &lt; num; i++ ) {
              // stuff
              loop();
          }
      }
  </code></pre>
  
  <p>so far i've made following bunch of code, but in this stage i get stuck... :</p>
  
  <pre><code>    Thread mainThread;
  
      List&lt;char&gt; azlower;
      List&lt;char&gt; azupper;
      List&lt;char&gt; nullnine;
  
      List&lt;char&gt; totalChars;
  
      int totalNum;
  
      int levelCounter;
  
      bool running;
  
      public Form1() {
          InitializeComponent();
      }
  
      private void init() {
          azlower = new List&lt;char&gt; { 'a', 'b', 'c' , 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
          azupper = new List&lt;char&gt; { 'A', 'B', 'C' , 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
          nullnine = new List&lt;char&gt; { '0', '1', '2' /* , '3', '4', '5', '6', '7', '8', '9' */ };
  
          totalChars = new List&lt;char&gt; ();
  
          running = false;
      }
  
      private void button1_Click( object sender, EventArgs e ) {
          if( !running ) {
              init();
  
              // Start
              if( checkBoxAZ1.Checked ) {
                  foreach( char character in azlower ) {
                      totalChars.Add( character );
                  }
              }
              if( checkBoxAZ2.Checked ) {
                  foreach( char character in azupper ) {
                      totalChars.Add( character );
                  }
              }
              if( checkBox09.Checked ) {
                  foreach( char character in nullnine ) {
                      totalChars.Add( character );
                  }
              }
              if( checkBoxS.Checked &amp;&amp; textBoxSpec.Text != "" ) {
                  char[] specArray = textBoxSpec.Text.ToCharArray();
                  foreach( char character in specArray ) {
                      totalChars.Add( character );
                  }
              }
              totalNum = totalChars.Count;
              levelCounter = Int32.Parse( textBoxMinLength.Text );
              mainThread = new Thread( new ThreadStart( run ) );
              button1.Text = "Stop";
              running = true;
              mainThread.Start();
          } else {
              mainThread.Abort();
              button1.Text = "Start";
              running = false;
          }
      }
  
      private void run() {
          for( int i = 0; i &lt; totalNum; i++ ) {
              Invoke( ( MethodInvoker ) delegate {
                  write( totalChars[ i ].ToString() );
                  if( i == totalNum - 1 &amp;&amp; levelCounter == Int32.Parse( textBoxMaxLength.Text ) ) {
                      write( "\n" );
                  }
              } );
              if( levelCounter &lt; Int32.Parse( textBoxMaxLength.Text ) ) {
                  levelCounter++;
                  run();
              }
          }
          return;
      }
  
      private void write( string line ) {
          richTextBox1.Text += line;
      }
  </code></pre>
  
  <p>but with the setup above, and my code, the output looks like this:</p>
  
  <pre><code>aabc
  bc
  </code></pre>
  
  <p>i think i've just made a thinking mistake, didn't i ?</p>
  
  <p>so guys, do you have anny suggestions for me ?</p>
  
  <p>i've also looked at the Cartesian Product, but i thought that it would not work with only one array...</p>
  
  <p>thx for any help.</p>