Closed
Description
Background and motivation
Add factory methods to allow creating instances of immutable dictionary types from dictionary expressions.
API Proposal
namespace System.Collections.Immutable
{
[CollectionBuilder(typeof(ImmutableDictionary), nameof(ImmutableDictionary.CreateRange))]
public sealed class ImmutableDictionary<TKey, TValue> : IDictionary<TKey, TValue> //, ...
{
// ...
}
public static class ImmutableDictionary
{
public static ImmutableDictionary<TKey, TValue> CreateRange<TKey, TValue>(
ReadOnlySpan<KeyValuePair<TKey, TValue>> collection)
where TKey : notnull;
public static ImmutableDictionary<TKey, TValue> CreateRange<TKey, TValue>(
IEqualityComparer<TKey> keyComparer,
ReadOnlySpan<KeyValuePair<TKey, TValue>> collection)
where TKey : notnull;
}
}
namespace System.Collections.Frozen
{
[CollectionBuilder(typeof(FrozenDictionary), nameof(FrozenDictionary.Create))]
public abstract class FrozenDictionary<TKey, TValue> : IDictionary<TKey, TValue> //, ...
{
// ...
}
public static class FrozenDictionary
{
public static FrozenDictionary<TKey, TValue> Create<TKey, TValue>(
ReadOnlySpan<KeyValuePair<TKey, TValue>> collection)
where TKey : notnull;
public static FrozenDictionary<TKey, TValue> Create<TKey, TValue>(
IEqualityComparer<TKey> comparer,
ReadOnlySpan<KeyValuePair<TKey, TValue>> collection)
where TKey : notnull;
}
}
API Usage
ImmutableDictionary<string, int> x = [with(StringComparer.Ordinal), "one":1, "two":2];
FrozenDictionary<string, int> y = [with(comparer: null), "three":3];
Alternative Designs
No response
Risks
No response