using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace TestCode
{
class Program
{
static void Main(string[] args)
{
MyDataCollection list = new MyDataCollection();
list.Add(new MyData("name1", 20));
list.Add(new MyData("name2", 15));
list.Add(new MyData("name3", 10));
list.Add(new MyData("name4", 5));
foreach (MyData data in list)
Console.WriteLine(string.Format("Name:{0} Age:{1}", data.Name, data.Age));
Console.ReadLine();
}
}
public sealed class MyDataCollection : Collection<MyData>
{
protected override void InsertItem(int index, MyData item)
{
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
base.RemoveItem(index);
}
protected override void SetItem(int index, MyData item)
{
base.SetItem(index, item);
}
protected override void ClearItems()
{
base.ClearItems();
}
}
public class MyData : IComparable<MyData>
{
private string _name;
private int _age;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public MyData(string name, int age)
{
_name = name;
_age = age;
}
public MyData()
{
}
#region IComparable<MyData> Members
int IComparable<MyData>.CompareTo(MyData other)
{
return _age.CompareTo(other._age);
}
#endregion
}
}