
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
312
|
Chapter 5: Collections
Discussion
The MaxMinValueDictionary class wraps the Dictionary<T,U> class so it can restrict
the range of allowed values. The overloaded constructor for the
MaxMinValueDictionary
class is defined here:
public MaxMinValueDictionary(int minValue, int maxValue)
This constructor allows the range of values to be set. Its parameters are:
minValue
The smallest integer value that can be added as a value in a key/value pair
maxValue
The largest integer value that can be added as a value in a key/value pair
The overridden indexer has both
get and set. The get returns the value that matches
the provided
key. The set checks the value parameter to determine whether it is
within the boundaries of the
minValue and maxValue fields before it is set.
The
Add method accepts an integer for its value parameter and performs the same
tests as the
set accessor on the indexer. If the test passes, the integer is added to the
MaxMinValueDictionary.
To modify the
MaxMinValueDictionary<T> class to accept numeric values other than
an integer, simply change all instances of the
int value parameter to another numeric
type, such as a
double or a long. Anytime you see Dictionary<T,int>, you will need to
change the
int type to the numeric type of your choosing. In addition, the minValue
and maxValue fields ...