C# foreach 循環遍歷數組


復制代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //聲明數組. 第一種方法. 聲明並分配元素大小. int[] Myint = new int[30]; Myint[0] = 30; Myint[1] = 50; // 以此類推, 起始下標為0 //------------------------------- // 聲明數組,第二種方法, 聲明並直接賦值,沒有指定元素大小. int[] Myint1 = { 20,10,50,65,18,90}; //------------------------------------------ //聲明數組,第三種方法, 聲明並分配大小,且賦值. int[] i = new int[5] { 10, 20, 30, 40, 50 }; // ----------------------------------------------- // foreach循環遍歷數組.. int[] Sum = new int[50]; Random rd = new Random(); // 先用for循環給數組取隨機數. for (int s = 0; s <= Sum.Length - 1; s++) // Sum.Length是數組的一個屬性,Length代表數組的長度  { Sum[s] = rd.Next(100); } // 遍歷數組輸出 foreach (int t in Sum) { Console.WriteLine(t); } } } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM