C#中数组的三种访问方式

来源:互联网 发布:淘宝买家秀的福利网盘 编辑:程序博客网 时间:2024/06/07 11:54

    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();        }    }}



0 0
原创粉丝点击