是键值对吗?不允许一对多?可以通过键找值,反过来呢?这容器里的对象不要求同一类型吧?给他添加一个元素不一定要 Add 吧?直接给个索引赋值就行了吧?
using System;
using System.Collections;
class IndexClass
{ Hashtable aa = new Hashtable(); public string this[int index]
{
get
{ return aa[index].ToString();
}
set
{ //aa[index] = value; //直接赋值
aa.Add(index, value);
}
} public int this[ string index]
{
get
{
return (int)aa[index];
}
set
{
// aa[index] = value;
aa.Add(index, value); }
}
}class Test_3
{
static void Main()
{
IndexClass ic = new IndexClass(); ic["1"] = 2;
ic[3] = "1";
Console.WriteLine(ic["1"]);
Console.WriteLine(ic[3]);
}
}
using System;
using System.Collections;
class IndexClass
{ Hashtable aa = new Hashtable(); public string this[int index]
{
get
{ return aa[index].ToString();
}
set
{ //aa[index] = value; //直接赋值
aa.Add(index, value);
}
} public int this[ string index]
{
get
{
return (int)aa[index];
}
set
{
// aa[index] = value;
aa.Add(index, value); }
}
}class Test_3
{
static void Main()
{
IndexClass ic = new IndexClass(); ic["1"] = 2;
ic[3] = "1";
Console.WriteLine(ic["1"]);
Console.WriteLine(ic[3]);
}
}
2.不允许;
3.可以通过键找值,反过来不行;
4.不要求同一类型,如果用泛型的话,就要同一类型;
5.一定要 Add;
6.不行