Interfaces in the Wild
The first situations where you’ll want to use interfaces involve applying pre-defined interfaces
and classes that are part of C#. In fact, you have already encountered implementations of several these
System
provided interfaces.
IComparer<T>
Purpose: A class implements this interface to compare two objects of a given class. This is
a parameterized interface, which means that when using it, you need to specify the class that
it will be comparing. For example, IComparer<Job>
would compare Job
objects.
Important Methods and Properties: Compare(T, T)
IComparer.Compare Documentation
This interface can be used to determine, given two objects of the given type, which one is greater than the other. It is also used by collections such as List to sort its contents with the Sort method.
Using IComparer<T>
is a two step process. You must first create a class that implements the interface.
Then, provide a custom implementation of the Compare
method:
Example
|
|
Compare(T, T)
returns an integer which determines which of the two objects comes before (in other
words, “is less than”) the other. If the returned value is less than zero, then the first parameter
comes before the second. If the integer is zero, then they are considered the same. If the integer is
greater than zero, then the second parameter comes before the first.
You can think of the result of calling Compare(x, y)
as being the value of subtracting, like
x - y
. If x
is smaller than y
, this value is negative. If x
is larger than y
,
this value is positive.
IEnumerable<T>
Purpose: Enable iteration over a collection of objects using
foreach
.
Important Methods and Properties: MoveNext()
, Current
This interface is implemented by the List<T>
class, which we’ve been
using throughout this course.
Example
|
|
Indeed, if you so desire, you may replace other instances of List
with this interface as long as those
interfaces don’t require specifically List
methods.
IList<T>
Purpose: Enable access to objects in a collection by index.
Important Methods and Properties: Add(T)
, Contains(T)
,
Remove(T)
, Count
This interface is also implemented by the List<T>
class, which we’ve
been using throughout this course. In fact, IList<T>
extends
IEnumerable<T>
. An interface may extend another interface, in the
same way that classes may extend each other.
Example
|
|
IDictionary<TKey, TValue>
Purpose: Represent a collection of key/value pairs.
Important Methods and Properties: Add(TKey, TValue)
,
Contains(T)
, Remove(T)
, Count
, Keys
, Values
This interface is implemented by the Dictionary<TKey, TValue>
class,
which we’ve been using throughout this course.
Example
|
|
Check Your Understanding
True or False: An interface can extend another interface.