using System; class ArrClass { //没有索引器的类 private readonly string name; public ArrClass(string name) { this.name = name; } public string Name { get { return name; } } } class IndexClass { //带索引器的类 private string[] name = new string[10]; public string this[int index] { get { return name[index]; } set { name[index] = value; } } } class test { static void Main() { //数组类的使用 ArrClass[] a = new ArrClass[10]; a[0] = new ArrClass("张三"); a[1] = new ArrClass("李四"); Console.WriteLine("a[0]" + a[0].Name); Console.WriteLine("a[1]" + a[1].Name); //索引器的使用 IndexClass b = new IndexClass(); b[0] = "王五"; b[1] = "赵六"; Console.WriteLine("b[0]" + b[0]); Console.WriteLine("b[1]" + b[1]); Console.ReadKey(); } }
索引器与数组类定义练习
时间: 2024-11-13 06:37:29