title: C# generating all combinations and permutations with n length for a string array
body: <p>this is a problem which I been facing for quite some time, ive searched all over the internet with many people asking simular questions to myself but never actually applicable in my case.
  the goal of my task is to use a string array and generate all permutations and combinations of a certain length. for example (handwritten so may not be 100% accurate)</p>
  <pre><code>string[] values = new string[] { &quot;ye&quot;, &quot;ok&quot;, &quot;nice&quot;, &quot;5&quot;);
  int length = 3;
  :
  yeoknice
  yeok5
  yeniceok
  yenice5
  ye5ok
  ye5nice
  okyenice
  okye5
  okniceye
  oknice5
  ok5nice
  ok5ye
  niceokye
  niceok5
  niceyeok
  niceye5
  nice5ok
  nice5ye
  5niceok
  5niceye
  5okye
  5oknice
  5yeok
  5yenice
  </code></pre>
  <p>also more values in the string array could be added like 100+ values in the string array and the length set to around 100 too.</p>
  <p>ive written some pheudocode:</p>
  <pre><code>string generatedvalue;
  int64 progress = 0;//so that if the generating takes along time the code can read from file and see the progress and start generating where it left off. i think if the amount of numbers its generating is big then the progress value could be big too maybe it would be good to support something higher like int512
  string[] values = new string[] { &quot;ye&quot;, &quot;ok&quot;, &quot;nice&quot;, &quot;5&quot;, &quot;ohyeah&quot;, &quot;rate&quot;, &quot;best&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;f&quot;);
  int length = 7;
  
  //generate all combinations and perumtations
  loop(conditions{
  progress++;
  
  //magic
  ? :(
  //
  generatedvalue = resultsfrommagic // put one of the combinations into the generatedvalue and write it to file so all the combinations and permutations are stored in a file on a new line
  
  //write to file
  writetofileappendnewline(progress + generatedvalue)
  }
  }
  </code></pre>
  <p>results from my online reserach:
  ive found a page which is very very close if not the answer to the question(except i dont know if its efficant and it doesnt accept string array only numbers) but its not in c# and the syntax is very strange so i cant begin to translate it or begin to include it in my work( since i dont understand that language it is possible that its not acutally the asnwer to the question anyway) <a href="https://elixirforum.com/t/generate-all-combinations-having-a-fixed-array-size/26196" rel="nofollow noreferrer">https://elixirforum.com/t/generate-all-combinations-having-a-fixed-array-size/26196</a> .</p>
  <p>what ive tried:
  ive created code which generates results from a string array using random numbers to randomly select an index from the string array and make a result from there eventually in theoery generating all the possible combinations through the random selecting however this is really really slow and potentially bad when it comes to generating the same values.</p>
  <p>the specific requirments of the string array, the permutations and the context makes this really difficult to find help online which can be used to help. i appreciate any help for this complex task!</p>