
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
106
|
Chapter 3: Classes and Structures
It is not necessary to change any of the derived classes or the driver methods
(
TestMediaABC and UseMedia).
See Also
See Recipe 3.16; see section 10.1.1.1, “Abstract Classes,” in the C# Language Speci-
fication.
3.5 Making a Type Sortable
Problem
You have a data type that will be stored as elements in an array or an ArrayList. You
would like to use the
Array.Sort and ArrayList.Sort methods to allow custom sort-
ing of your data types in the array. In addition, you may need to use this type in a
SortedList collection.
Solution
Implement the IComparable interface. The Square class shown in Example 3-5 imple-
ments this interface in such a way that the
Array, ArrayList, and SortedList objects
can sort and search an array or collection of these
Square objects.
public void WriteTo(string data)
{
throw new NotImplementedException( );
}
public string ReadFrom( )
{
throw new NotImplementedException( );
}
public void Close( )
{
}
private IntPtr mediaHandle = IntPtr.Zero;
public IntPtr Handle
{
get {return(mediaHandle);}
}
}
Example 3-4. Implementing a concrete base class (Media) (continued)