本文實(shí)例講述了C#使用foreach循環(huán)遍歷數(shù)組的方法。分享給大家供大家參考,具體如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{  class Program  {    static void Main(string[] args)    {      //聲明數(shù)組. 第一種方法. 聲明并分配元素大小.      int[] Myint = new int[30];      Myint[0] = 30;      Myint[1] = 50;      // 以此類推, 起始下標(biāo)為0      //-------------------------------      // 聲明數(shù)組,第二種方法, 聲明并直接賦值,沒有指定元素大小.      int[] Myint1 = { 20,10,50,65,18,90};       //------------------------------------------      //聲明數(shù)組,第三種方法, 聲明并分配大小,且賦值.      int[] i = new int[5] { 10, 20, 30, 40, 50 };      // -----------------------------------------------      // foreach循環(huán)遍歷數(shù)組..      int[] Sum = new int[50];      Random rd = new Random();      // 先用for循環(huán)給數(shù)組取隨機(jī)數(shù).      for (int s = 0; s <= Sum.Length - 1; s++) // Sum.Length是數(shù)組的一個(gè)屬性,Length代表數(shù)組的長(zhǎng)度      {        Sum[s] = rd.Next(100);      }      // 遍歷數(shù)組輸出      foreach (int t in Sum)      {        Console.WriteLine(t);      }    }  }}希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注