Does C# support foreach loop?

42. Does C# support foreach loop?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Yes, C# supports foreach loop. The foreach loop is used to iterate over the elements of the collection. The collection may be an array or a list.

Example:

using System;
class Program {
  static void Main(string[] args) {
    // an array
    int[] arr = new int[] {10, 20, 30, 40, 50};

    foreach(int elements in arr) {
      Console.WriteLine(elements);
    }
  }
}
/*
Output:
10
20
30
40
50
*/

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.