TV>
is dynamic and will automatically grow as needed.
SortedList
is similar to
SortedDictionary
but has different performance characteristics. For example, a
SortedList
uses less memory, but a
SortedDictionary
is faster when
inserting out-of-order elements.
SortedList
provides many constructors. Here is a sampling:
public SortedList( )
public SortedList(IDictionary
dict
)
public SortedList(int
capacity
)
public SortedList(IComparer
comp
)
The first constructor creates an empty list with a default capacity. The second creates a list
that contains the same elements as those in
dict.
The third lets you specify an initial capacity.
If you know in advance that you will need a list of a certain size, then specifying that capacity
will prevent the resizing of the list at runtime, which is a costly process. The fourth form lets
you specify a comparison method that will be used to compare the objects contained in the list.
The capacity of a
SortedList TV>
list grows automatically as needed when elements
are added to a list. When the current capacity is exceeded, the capacity is increased. The
advantage of specifying a capacity is that you can prevent or minimize the overhead
associated with resizing the collection. Of course, it makes sense to specify an initial
capacity only if you have some idea of how many elements will be stored.
In addition to the methods defined by the interfaces that it implements,
SortedList
TV>
also defines several methods of its own. A sampling is shown in Table 24-18. Notice
that the enumerator returned by
GetEnumerator( )
enumerates the key/value pairs stored
in the list as objects of type
KeyValuePair
.
Method
Description
public void Add(TK
k
, TV
v
)
Adds the key/value pair specified by
k
and
v
to
the list. If
k
is already in the list, then its value is
unchanged and an
ArgumentException
is thrown.
public bool ContainsKey(TK
k
)
Returns true if
k
is a key in the invoking list. Returns
false other wise.
public bool ContainsValue(TV
v
)
Returns true if
v
is a value in the invoking list.
Returns false other wise.
public IEnumerator>
GetEnumerator( )
Returns an enumerator for the invoking list.
public int IndexOfKey(TK
k
)
Returns the index of the key specified by
k.
Returns
–1 if the key is not in the list.
T
ABLE
24-18
Several Commonly Used Methods Defi ned by
SortedList
www.freepdf-books.com
Do'stlaringiz bilan baham: