foreach loop is a different kind of looping constructs in C# programming
that doesn’t includes initialization, termination and
increment/decrement characteristics. It uses collection to take value
one by one and then processes them.
syntax:
foreach (string name in arr)
{
Where, name is a string variable that takes value from collection as arr and then processes them in the body area.
Output
Hello Aav
Hello Aditya
Hello Rohit
Hello Ranvijay
Hello Tarak
syntax:
foreach (string name in arr)
{
}
Where, name is a string variable that takes value from collection as arr and then processes them in the body area.
using System;
namespace foreach_loop
{
class Program
{
static void Main(string[] args)
{
string[] arr = new string[5]; // declaring array
//Storing value in array element
arr[0] = "Aav";
arr[1] = "Aditya";
arr[2] = "Rohit";
arr[3] = "Ranvijay";
arr[4] = "Tarak";
//retrieving value using foreach loop
foreach (string name in arr)
{
Console.WriteLine("Hello " + name);
}
Console.ReadLine();
}
}
}
namespace foreach_loop
{
class Program
{
static void Main(string[] args)
{
string[] arr = new string[5]; // declaring array
//Storing value in array element
arr[0] = "Aav";
arr[1] = "Aditya";
arr[2] = "Rohit";
arr[3] = "Ranvijay";
arr[4] = "Tarak";
//retrieving value using foreach loop
foreach (string name in arr)
{
Console.WriteLine("Hello " + name);
}
Console.ReadLine();
}
}
}
Output
Hello Aav
Hello Aditya
Hello Rohit
Hello Ranvijay
Hello Tarak
Comments
Post a Comment