C#中数组的三种访问方式
C#中数组有一维数组、二维数组、交错数组(变长数组)、三维数组.......等多种方式。而对数组的访问可以通过for循环、foreach、Enumerator等方式进行访问。
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace ArrayAccess { class Program { static void Main(string[] args) { int[] myArray = { 1, 2, 3, 4, 5 }; for (int i = 0; i < myArray.Length; i++) Console.Write("{0,5}", myArray[i]); Console.WriteLine(); foreach(int i in myArray) Console.Write("{0,5}", i); Console.WriteLine(); IEnumerator enumerator = myArray.GetEnumerator(); while (enumerator.MoveNext()) { Console.Write("{0,5}", enumerator.Current); } Console.WriteLine(); Console.ReadLine(); } } }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: c#交错数组
- 下一篇: node.js之基础加密算法模块crypto