
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Creating a Priority Queue
|
611
This special comparer is required because you want to prioritize the elements in the
queue by size. The default string
IComparer<string> interface compares strings alpha-
betically. Implementing the
IComparer<T> interface requires that you implement a sin-
gle method,
Compare, with the following signature:
int Compare(T x, T y);
where x and y are the objects being compared. When implementing custom Compare
methods, the method is to return 0 if x equals y, less than 0 if x is less than y, and
greater than
0 if x is greater than y. This method is called automatically by the .NET
runtime whenever the custom
IComparer<T> implementation is used.
See Also
See the “List<T> Class,” “IEnumerable Interface,” “ICloneable Interface,”
“IComparer<T> Interface,” and “IComparable<T> Interface” topics in the
MSDN documentation.
}
else if (str1.Length > str2.Length)
{
return (1);
}
else
{
return (-1);
}
}
public bool Equals(T item1, T item2)
{
if (item1 == null || item2 == null)
{
throw(new ArgumentNullException(
"The objects being compared may not be null."));
}
return (item1.Equals(item2));
}
public int GetHashCode(T obj)
{
if (obj == null)
{
throw(new ArgumentNullException(
"The obj parameter may not be null."));
}
return (obj.GetHashCode( ));
}
}
Example 11-3. Special CompareLen ...