
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
116
|
Chapter 3: Classes and Structures
Sorted ArrayList using IComparable
Height:2 Width:1
Height:1 Width:3
Height:6 Width:1
Height:4 Width:3
Search using IComparable
found (6,1): 2
SORTEDLIST
0 : Height:1 Width:3
1 : Height:2 Width:1
2 : Height:4 Width:3
3 : Height:6 Width:1
SortedListOfSquares.Contains(2): True
SortedListOfSquares.ContainsKey(2): True
SortedListOfSquares.ContainsValue(new Square(6,1)): False
See Also
See Recipe 3.5; see the “IComparable Interface” and “IComparer Interface” topics in
the MSDN documentation.
3.7 Indirectly Overloading the +=, –=, /=,
and *= Operators
Problem
You need to control the handling of the +=, –=, /=, and *= operators within your data
type; unfortunately, these operators cannot be directly overloaded.
Solution
Overload these operators indirectly by overloading the +, –, /, and * operators, as
demonstrated in Example 3-9.
Example 3-9. Overloading the +, –, /, and * operators
public class Foo
{
// Other class members...
// Overloaded binary operators
public static Foo operator +(Foo f1, Foo f2)
{
Foo result = new Foo( );