Foreach loop to iterate through the elements of the array in C#

int[] intArray = { 1, 2, 3, 4, 5 }; 
Console.WriteLine("Upper Bound"); 
Console.WriteLine(intArray. GetUpperBound(0) ); 
Console.WriteLine("Array elements") ; 
foreach (int item in intArray) 
{ 
	Console.WriteLine(item) ; 
} 
Array.Reverse(intArray) ; 
Console.WriteLine("Array reversed") ; 
foreach (int item in intArray) 
{ 
	Console.WriteLine(item) ; 
} 
Array.Clear(intArray, 2, 2) ; 
Console.WriteLine("Elements 2 and 3 cleared") ; 
foreach (int item in intArray) 
{ 
	Console.WriteLine(item) ; 
} 
intArray[ 4] = 9; 
Console.WriteLine("Element 4 reset"); 
foreach (int item in intArray) 
{ 
	Console.WriteLine(item) ; 
} 
Console.ReadLine();

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.