111,119
社区成员
发帖
与我相关
我的任务
分享
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public double Height
{
get;
set;
}
}
public class PersonList : List<Person> //不用泛型可以用CollectionBase
{
public new Person this[int age]
{
get
{
return GetByAge(age);
}
set
{
Person p = GetByAge(age);
if (GetByAge(age) != null)
{
p = value;
}
else
{
throw new Exception("There is No Person Age IS Value");
}
}
}
private Person GetByAge(int age)
{
List<Person> list = this.Where(p => p.Age == age).ToList();
if (list.Count > 0)
{
return list[0];
}
else
{
return null;
}
}
//public Person this[string name]
//{
// get
// {
// }
// set
// {
// }
//}
//public Person this[double height]
//{
// get
// {
// }
// set
// {
// }
//}
}
class Test
{
public class Table
{
private Rows rows;
public Table()
{
rows = new Rows();
}
public Rows Rows
{
get { return rows; }
set { rows = value; }
}
}
public class Rows
{
public Hashtable hash = new Hashtable();
public Rows()
{
hash.Add(0, 100);
hash.Add("OK", 200);
}
public object this[int i]
{
get { return hash[i]; }
set { hash[i] = value; }
}
public object this[string s]
{
get { return hash[s]; }
set { hash[s] = value; }
}
}
static void Main()
{
Table t = new Table();
Console.WriteLine(t.Rows[0].ToString());
Console.WriteLine(t.Rows["OK"].ToString());
}
}