I had a problem, I need to reset the variable when it exceeds the length of the array.

for example

int i=0; do { // нужно, чтобы шло перечисление до конца массива, но когда i=content.Length // i стало равно 0, перечисление продолжилось с начала массива и до его конца // и так пока i = content.Lentgh ICr nextGen = content [i]; i++; } while(content.Count !=0); 
  • Do you fundamentally use a do while loop? - koks_rs
  • inside there is another code, and this is the best option - FlyFox

1 answer 1

To iterate over the elements of an array, it is most convenient to use the for loop:

 for (var i = 0; i < content.Length; i++) { ICr nextGen = content[i]; ... } 

If you are not going to modify the array, then it is wise to use a foreach :

 foreach (ICr nextGen in content) { ... } 

Solution with do-while . Note that if you do not change the number of array elements in this loop, then the loop will be infinite (because content.Count != 0 will always be true ).

 int i = 0; do { if (i == content.Length) { i = 0; } ICr nextGen = content[i]; i++; } while(content.Count != 0); 
  • I understand this, but when i = c.Length, the cycle is over., but I need it to re-run through the array to its end - FlyFox
  • @FlyFox updated. Maybe put more information on the problem, maybe we 'll give a better solution ... - MihailPw
  • Thanks, the last one helped) - FlyFox
  • Than foreach is better than for for bypass in case you don't need to modify the masses? - AK