int[] myArray;//声明
myArray = new int[4];//初始化int[] myArray = new int[4];//声明+初始化int[] myArray = new int[4]{4,8,9,9};int[] myArray ={4,8,9,6};
自定义类型的数组
public class Person//定义person类{
public string FirstName{get;set;}
public string LastName{get;set;}
public override string ToString(){return string.Foramte("{0} {1}",FirstName,LastName);}}
Person[] myPerson = new Person[2];
myPerson[0]= new Person{FirstName ="AA",LastName ="BB"};
myPerson[1]= new Person{FirstName ="CC", LastName ="DD"};
多维数组
int[,] twodim = new int[3,3];int[,] twodim ={{1,2,3},{4,5,6},{7,8,9}};int[,,] threedim ={{{1,2},{3,4}},{{5,6},{7,8}},{{9,10},{11,12}}};
锯齿数组
int[][] jagged = new int[3][];
jagged[0]= new int[2]{1,2};
jagged[1]= new int[4]{1,2,3,4,};
jagged[2]= new int[2]{4,5};//历遍for(int row =0;row<jagged.Length;row++){for(int element =0;element<jagged[row].Lenght; element++){}}
using System;
using System.Collections;
namespace Program
{staticvoidMain(string[] args){
var helloCollection = new HelloCollection();foreach(var s in helloCollection){
Console.WriteLine(s);}
Console.ReadKey();}
public class HelloCollection
{
public IEnumerator<string>GetEnumerator(){
yield return"Hello";
yield return"World";}}}/* result:
Hello
World
*/