iterate an array in csharp

You can iterate through an array in C# using a for loop. Here's an example:

main.cs
int[] myArray = {1, 2, 3, 4, 5};

for(int i = 0; i < myArray.Length; i++)
{
    Console.WriteLine(myArray[i]);
}
113 chars
7 lines

This will output each element in the array on a new line:

main.cs
1
2
3
4
5
10 chars
6 lines

You can also use foreach loop to iterate through an array in C#. Here's an example:

main.cs
int[] myArray = {1, 2, 3, 4, 5};

foreach(int element in myArray)
{
    Console.WriteLine(element);
}
102 chars
7 lines

This will also output each element in the array on a new line:

main.cs
1
2
3
4
5
10 chars
6 lines

gistlibby LogSnag