Alternate Dictionary Lookup
08/08/2024
This blog post is based on .NET 9 Preview 6. Bits and pieces of the underlying runtime will change for the final release.
In .NET9 a new extension method on Dictionary
type allows creating an alternate lookup type for the dictionary. This is particularly useful for dictionaries that have a string
key, as the alternate lookup allows an alternate type to be used for lookups in the dictionary, for example ReadOnlySpan<char>
.
Most sets and dictionaries (immutable, frozen, etc.) support this new API. However, not all dictionaries support the alternate lookup equally. Some only support certain types as alternates. Hence, there is a second extension method TryGetAlternateLookup
, which does not throw when the requested alternate type is not supported.