Why is the property SortedList (TKey, TValue) .Keys is IList (TKey) and not ReadOnlyCollection (TKey)?

The interface IList<T>includes index access in addition to operations not supported by the property SortedList<TKey, TValue>.Keys, such as Add, Removeand Insert.

A ReadOnlyCollection<T>, for example, the return value List<T>.AsReadOnly, implements IList<T>and therefore offers access by index , but hides illegal operations, such as Addetc., implementing them explicitly , In addition, this is just a wrapper for the main list; therefore it does not create a copy and therefore (I would assume) would not suffer any real performance hit.

Any idea why SortedList<TKey, TValue.Keysnot ReadOnlyCollection<TKey>? (And in this case, why is the property Valuesnot ReadOnlyColllection<TValue>?)

+3
source share
1 answer

This is pretty obscure, but I think it is an optimization. This has something to do with how generic funds are implemented. The machine code for the generic class method is generated at runtime by the JIT compiler. He must make several specific versions. There is one for any reference type. And one for each argument of the type of value that is used in the program.

, , . , Ngen-ed. ​​JIT Ngen.

, (, , ), . do-nothing, . , Ngen.exe . , Ngen, JIT .

, , System.Collections.ObjectModel.ReadOnlyCollection, , , . , , , , , Reference Source.pdbs.

100%, . .

+3

Source: https://habr.com/ru/post/1735275/


All Articles