I only found that SortedList contains IsFixedSize property but I can't find answer to that simple question anywhere.
This property is something inherited from IDictionary
, since SortedList
implements it. This property will always return false
for the default implementation of SortedList
(as stated in the msdn documentation), simply because the default implementation is not fixed size.
Fixed size means that the collection disallows any Add
or Remove
type operations (anything that would change the underlying collection, not only the contained elements) after it's constructed via a wrapper (again, the docs). So if you want a SortedList
of fixed size, you should create your own wrapper, something like
public class SortedListFixed<TKey, TValue> : SortedList<TKey, TValue>
{
private SortedList<TKey, TValue> _list;
public bool IsFixedSize => true;
/** ctors **/
public void Add(TKey key, TValue value) =>
throw InvalidOperationException("This collection is fixed size");
public bool Remove (TKey key) =>
throw InvalidOperationException("This collection is fixed size");
/** etc. for all inherited size-modifying methods **/
}
Two ctors would come in handy, one that takes a capacity, creates the underlying _list
with that capacity, and a wrapping ctor taking an existing SortedList
as a parameter. You could also combine the latter with a neat extension method like this:
public static class SortedListExtensions
{
public static SortedListFixed<TKey, TValue> ToFixedSize<TKey, TValue>(
this SortedList<TKey, TValue> list) => new SortedListFixed<TKey, TValue>(list);
}
Note that it's more a guideline than a whole implementation.