Using Foreach Loop to Access an Array

Using Foreach Loop to Access an Array

Here lets create an array of Fruits (of same data type string)

Name of Our Array is mystrArray and it has 5 elements.

 

string[] mystrArray = new string[] { “Apple”, “Banana”, “Grapes”, “Mango”, “Pine-Apple” };
 
// Reading array items using foreach loop
foreach (string mystr in mystrArray)
{
    Console.WriteLine(mystr);
}

A loop will iterate through all the elements in the string and Print them one by one as we are referring to Each element of the array.
Foreach loop is very handy when the requirement is to fetch multiple rows of records from a database.

Social Share

Leave a Reply

Your email address will not be published. Required fields are marked *