C uses the foreach loop to iterate through complete instances of an array

  • 2021-10-25 07:38:57
  • OfStack

This article illustrates how C # uses foreach loops to iterate through arrays. Share it for your reference, as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      // Declare an array .  No. 1 1 Methods .  Declare and assign element sizes .
      int[] Myint = new int[30];
      Myint[0] = 30;
      Myint[1] = 50;
      //  And so on ,  The initial subscript is 0
      //-------------------------------
      //  Declare an array , No. 1 2 Methods ,  Declare and assign values directly , Element size not specified .
      int[] Myint1 = { 20,10,50,65,18,90}; 
      //------------------------------------------
      // Declare an array , No. 1 3 Methods ,  Declare and assign sizes , And assign a value .
      int[] i = new int[5] { 10, 20, 30, 40, 50 };
      // -----------------------------------------------
      // foreach Loop through an array ..
      int[] Sum = new int[50];
      Random rd = new Random();
      //  First use for Loop to take random numbers to arrays .
      for (int s = 0; s <= Sum.Length - 1; s++) // Sum.Length Is the of an array 1 Attributes ,Length Represents the length of the array 
      {
        Sum[s] = rd.Next(100);
      }
      //  Traversing Array Output 
      foreach (int t in Sum)
      {
        Console.WriteLine(t);
      }
    }
  }
}

More readers interested in C # can check the topic of this site: "C # Traversal Algorithms and Skills Summary", "C # Programming Thread Use Skills Summary", "C # Operating Excel Skills Summary", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: