索引器2


namespace _03
{
class Program
{

//请编写一个类:ItcastClass,该类中有一个私有字段_names,数据类型为:字符串数组,长度为5,并且有5个默认的姓名。
//要求:为ItcastClass类编写一个索引器,要求该索引器能够通过下标来访问_names中的内容

static void Main(string[] args)
{
ItcastClass it = new ItcastClass();
it[3] = "8";
Console.WriteLine(it[2]);
Console.WriteLine(it[3]);
Console.ReadKey();
}


}
class ItcastClass
{
private string[] _names = { "1", "2", "3", "4", "5" };
public string[] Names
{
get { return _names;}
set {_names=value;}

}
public string this[int index]
{
get { return _names[index]; }
set { _names[index] = value; }
}
}
}

posted @ 2016-12-07 16:51  温柔牛  阅读(99)  评论(0编辑  收藏  举报