ImmutableDictionary
有一个 TryAdd
方法,它从 System.Collections.Generic.CollectionExtensions
获取(出于某种原因我无法在点网点)
所以编译:
ImmutableDictionary<int, int> d = ImmutableDictionary<int, int>.Empty;
bool b = d.TryAdd(1, 1);
但是我不明白它的作用?它不应该返回一个新的 ImmutableDictionary 吗?
最佳答案
ImmutableDictionary.Add确实返回一个新的 ImmutableDictionary 值。您所指的扩展方法看起来像是 from here .
我不建议将此扩展方法与 Immutable 字典一起使用,因为虽然这可以编译,但您将得到 NotSupportedException
在运行时。
进一步观察,according to this, IDictionary<T,TR>.Add
的显式接口(interface)实现只会抛出 NotSupportedException
,这似乎是合适的。
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
throw new NotSupportedException();
}
关于c# - 不可变字典尝试添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51850940/