0

looping through arrays

is there a way in which you can loop through arrays in c#?

20th Apr 2018, 6:47 PM
robert kiprotich
robert kiprotich - avatar
1 Answer
0
Of course theres a few ways but the two most common ones: for (int i = 0; i < arrayName.length; i++) { Console.WriteLine(arrayName[i]); } This one works by looping through numbers 0 (start) -> the arrays length which the i variable stores and then accesses it through [i]. For this example lets say the array is type of int. foreach (int num in arrayName) { Console.WriteLine(num); } The foreach example does the indexing for you. So every iteration the loop changes the num variable to the value in the array if this was our array: int[] arr = {1, 2, 3}; At our first iteration num would be 1, then 2 and finally 3 then exit the foreach loop.
20th Apr 2018, 7:07 PM
TurtleShell
TurtleShell - avatar