//用索引取一个记录中的各项
using system;
class IndexerRecord{
private string[] data= new string [6];
private string[] keys = {
"Author", "Publisher", "Title",
"Subject", "ISBN", "Comments"
};
//程序中用了两种方法索引,一是整数作下标,一是关键字(字符串)作下标
public string this [int idx]{
set
{
if( idx>=0 && idx<data.length )
data[idx]=value;
}
get
{
if(idx>=0 && idx <data.length)
return data[idx];
return NULL;
}
}
public string this[string key]
{
set
{
ind idx = FindKey(key);
this[idx] = value;
}
get
{
return this[FindKey(key)];
}
}
private int FindKey(string key)
{
for(int i=0;i<keys.length;i++)
if(keys[i] == key)return i;
return -1;
}
static void Main()
{
IndexRecord record = new IndexRecord();
record[0] = "马克 吐温";
record[1] = "Crox出版公司";
record[2] = "汤姆 索亚历险记";
Console.WriteLine(record["Title"]);
Console.WriteLine( record[ "Author" ] );
Console.WriteLine( record[ "Publisher" ] );
}
}
//属性
class Person
{
public string Name{set;get;}
}