Check out example codes for "c# foreach". It will help you in understanding the concepts better.
Code Example 1
foreach (string s in sList)
{
if (s.equals("ok"))
{
break; // get out of the loop
}
}
Code Example 2
var fibNumbers = new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 };
int count = 0;
foreach (int element in fibNumbers)
{
count++;
Console.WriteLine($"Element #{count}: {element}");
}
Console.WriteLine($"Number of elements: {count}");
Code Example 3
var fibNumbers = new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 };
int count = 0;
foreach (int element in fibNumbers)
{
count++;
Console.WriteLine($"Element #{count}: {element}");
}
Console.WriteLine($"Number of elements: {count}");
Code Example 4
//No counter just the foreach.
//the HelloWorld is the Class objects name nor a public string can be used aswell.
foreach (string element in fibNumbers)
{
Console.WriteLine(element.HelloWorld);
}
Code Example 5
foreach (int num in numbers)
{
}
Code Example 6
var numbers = new NumberList<int> { 0, 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
DoSomething();
}
Learn ReactJs, React Native from akashmittal.com