Foreach C#

A program iterates over a collection. The index of each element is not needed. Only the elements are needed. With foreach we access just the elements. A foreach-loop, this is the easiest, least error-prone loop. It is preferred in many program contexts. Here are some samples: using System; class Program { static void Main() { string[] pets = { “dog”, “cat”, “bird” }; // … Loop with the foreach keyword. foreach (string value in pets) { Console.WriteLine(value); } }…

Read More