class Inventory { string name; double cost; int onhand;
public Inventory(string n, double c, int h) { name = n; cost = c; onhand = h;
}
public override string ToString() { return
String.Format ("{0,-10}: {1,6:} : {2}", name, cost, onhand);
}
}
class TypeSafelnventoryList { static void Main() {
List<Inventory> inv = new List<Inventory> ();
// . inv.Add(new Inventory("", 5.95, 3)); inv.Add(new Inventory("", 8.29, 2)); inv.Add(new Inventory("", 3.50, 4)); inv.Add(new Inventory("", 19.88, 8));
Console.WriteLine(" :"); foreach(Inventory i in inv) {
Console.WriteLine(" " + i);
}
}
}
' Inventory List<T>. . , , , , .
: . , , , , 40 , . , , . , .
: . , ArrayList List<T> Inventory. . -, Inventory IComparable, . -, IComparer. .
IComparable
, , , SortedList, , . , , IComparable . IComparable : . , , , , .
|
|
IComparable
, , IComparable. , CompareTo (), . CompareTo ().
int CompareTo(object obj)
CompareTo () obj. , ; , obj; , obj. . obj , CompareTo () ArgumentException.
IComparable. IComparable Inventory, . Inventory CompareTo () name , . , Inventory IComparable .
// IComparable.
Using System;
Using System.Collections;
// IComparable. class Inventory: IComparable { string name; double cost; int onhand;
public Inventory(string n, double c, int h) { name = n; cost = c; onhand = h;
}
public override string ToStringO { return
String.Format ("{0,-10}: {1,6:} : {2}", name, cost, onhand);
}
// IComparable. public int CompareTo(object obj) {
Inventory b;
b = (Inventory) obj;