索引!
这个部分内容,一般重要。
//索引
参照属性 --> 字段成员 1
索引 --> 集合成员 n
class Animal
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string[] names = {"张华","小华","华仔","zhanghua" };
public string this[int index]
{
get { return names[index]; }
set { names[index] = value; }
}
}
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string[] names = {"张华","小华","华仔","zhanghua" };
public string this[int index]
{
get { return names[index]; }
set { names[index] = value; }
}
}
索引的使用,写在main方法里
Animal a = new Animal();
a[0] = "张小华"; //读
Console.WriteLine(a[2]); //写
a[0] = "张小华"; //读
Console.WriteLine(a[2]); //写