For example, n = 2, then the lines:

"" "0" "1" "00" "01" "10" "11" 

There is no need to store already scanned strings. At the moment, I am generating by adding '0' and '1' to all rows of the previous dimension, storing everything in the List. It works fast, but takes a lot of space, and the List is not infinite.

    2 answers 2

    In my opinion, the easiest way is to run the entire required range of numbers from 0 to 1 << n (which is 2 to the power n ), to get a string on the fly using the Convert.ToString method indicating the base of the number system 2 :

     int n = 3; int maxNumber = 1 << n; for (int i = 0; i < maxNumber; i++) { Console.WriteLine(Convert.ToString(i, 2)); } 
       int MAX_LENGTH = 10; for(long i = 0; ; i++) { string s = Convert.ToString(i, 2); if (s.Length > MAX_LENGTH) break; Console.WriteLine(s); }