//索引器类
class IndexCls
{
Hashtable ht = new Hashtable();
public string this[int idx]
{
get
{
return (string)ht[idx];
}
set
{
ht.Add(idx, value);
}
}
public override string ToString()
{
return "this's IndexCls";
}
}
//测试类
public class TestCls
{
static void Main(string[] args)
{
//IndexCls ic = new IndexCls();//new一个索引器类
//IndexCls[] ic = new IndexCls[2] { new IndexCls(), new IndexCls() };//创建数组
//ic[1] = "aaaaa";
Console.WriteLine(ic[1]);//到底结果是如何呢?
Console.ReadLine();
}
}
class IndexCls
{
Hashtable ht = new Hashtable();
public string this[int idx]
{
get
{
return (string)ht[idx];
}
set
{
ht.Add(idx, value);
}
}
public override string ToString()
{
return "this's IndexCls";
}
}
//测试类
public class TestCls
{
static void Main(string[] args)
{
//IndexCls ic = new IndexCls();//new一个索引器类
//IndexCls[] ic = new IndexCls[2] { new IndexCls(), new IndexCls() };//创建数组
//ic[1] = "aaaaa";
Console.WriteLine(ic[1]);//到底结果是如何呢?
Console.ReadLine();
}
}
以上代码 执行之后 你觉得c# 会调用 索引器还是数组呢?~答案当然是数组啦~