Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0041 for loop and array

We can use for loop the iterate each element in an array. using System; class Program { static void Main(string[] args) { int[] intArray = new int[10]; for (int i = 0; i < intArray.Length; i++) { intArray[i] = i; } for (int i = 0; i < intArray.Length; i++) { Console.WriteLine(intArray[i]); } } } The output: 0 1 2 3 4 5 6 7 8 9 The length property of the array stores the element count.