public string this[int index] { get {return ""; } set { } } using System; using System.Collections.Generic; using System.Text; namespace 索引 { class Program { static void Main(string[] args) { int[] values ={ 1,5,6}; int i=values[1];; //数组索引 Person p=new Person(); p[1]="小明"; Console.WriteLine(p[1]+p[2]); Console.WriteLine(p["NIKE",1,2]); Console.ReadKey(); } } class Person { private string FirstName = "甲"; private string SecondName = "乙"; public string this[string name, int x, int y] //索引可以重载,string是索引器的数据类型 { get //可以只有get { return name + x + y; } } public string this[int index] { set { if(index==1) { FirstName=value; } else if(index==2) { SecondName=value; } else { throw new Exception("错误的程序 "); } } get { if(index==1) { return FirstName; } else if(index==2) { return SecondName; } else { throw new Exception("错误的程序 "); } } } } }