int
,
string
, and
double
. It
is also easy to implement for classes that you create.
The
IComparable
interface defines only the
CompareTo( )
method shown here:
int CompareTo(object
obj
)
CompareTo( )
compares the invoking object to
obj.
It returns zero if the two objects are
equal, a positive value if the invoking object is greater than
obj,
and a negative value if the
invoking object is less than
obj.
To use
CompareTo( )
, you must specify a constraint that requires every type argument
to implement the
IComparable
interface. Then, when you need to compare two instances of
the type parameter, simply call
CompareTo( )
. For example, here is a corrected version of
IsIn( )
:
// Require IComparable interface.
public static bool IsIn(T what, T[] obs) where T : IComparable {
foreach(T v in obs)
www.freepdf-books.com
Do'stlaringiz bilan baham: |