PART II
C h a p t e r 2 4 :
C o l l e c t i o n s , E n u m e r a t o r s , a n d I t e r a t o r s
805
Console.WriteLine("Inventory list after sorting:");
foreach(Inventory i in inv) {
Console.WriteLine(" " + i);
}
}
}
This program produces the same output as the previous, non-generic version.
IComparer'>Using an IComparer
Although implementing
IComparable
for classes that you create is often the easiest way to
allow objects of those classes to be sorted, you can approach the problem in a different way
by using
IComparer
. To use
IComparer
, first create a class that implements
IComparer
, and
then specify an object of that class when comparisons are required.
There are two versions of
IComparer
: generic and non-generic. Although the way each
is used is similar, there are some small differences, and each approach is examined here.
Do'stlaringiz bilan baham: |