要创建空序列,请使用以下
var empty = Enumerable.Empty<string> ();
是否存在与此一样容易创建空字典的等价物?
答案 0 :(得分:27)
没有等效的......
Enumerable.Empty<T>()
的目的是返回空数组的“缓存”实例。因此,您可以避免创建新数组(return new T[0];
)的开销。
您无法将此转换为非{1}}或IDictionary<TKey, TValue>
之类的非只读结构,因为返回的实例可能会在以后修改,因此会使目的无效......
答案 1 :(得分:9)
new Dictionary<string, string>()
出了什么问题?
答案 2 :(得分:5)
我认为(至少现在5年后)空字典实际上意味着空只读字典。此结构与空的可枚举序列一样有用。例如,您可能有一个具有字典属性(想想JSON)的配置类型,一旦配置它就无法修改:
public class MyConfiguration
{
public IReadOnlyDictionary<string, string> MyProperty { get; set; }
}
但是,如果从未配置该属性该怎么办?然后MyProperty
为null
。避免意外NullReferenceException
的一个好方法是使用空字典初始化属性:
public class MyConfiguration
{
public IReadOnlyDictionary<string, string> MyProperty { get; set; }
= new Dictionary<string, string>();
}
缺点是MyConfiguration
的每次分配都需要分配一个空字典。为避免这种情况,您需要类似Enumerable.Empty<T>()
的内容,即缓存的空只读字典。
有两种方法可以实现这一目标。第一个是依赖System.Collections.Immutable。 ImmutableDictionary<TKey, TValue>
实施IReadOnlyDictionary<TKey, TValue>
,并且您可以使用Empty
字段:
IReadOnlyDictionary<string, string> empty = ImmutableDictionary<string, string>.Empty;
或者您可以实现类似于Enumerable.Empty<T>()
和Array.Empty<T>()
的自己的空白只读字典。注意空值不再是一个字段,而且该类不是通用的。相反,它是一种通用方法。这需要两个类。
第一堂课是“隐藏的”,可以是内部的:
internal static class EmptyReadOnlyDictionary<TKey, TValue>
{
public static readonly IReadOnlyDictionary<TKey, TValue> Instance
= new Dictionary<TKey, TValue>();
}
第二个类使用第一个类但隐藏在IReadOnlyDictionary<TKey, TValue>
接口后面:
public static class ReadOnlyDictionary
{
public static IReadOnlyDictionary<TKey, TValue> Empty<TKey, TValue>()
=> EmptyReadOnlyDictionary<TKey, TValue>.Instance;
}
用法:
IReadOnlyDictionary<string, string> empty = ReadOnlyDictionary.Empty<string, string>();
对于这两种解决方案,TKey
和TValue
的每个不同组合只会有一个空字典实例。
答案 3 :(得分:1)
回到2019年,有一种方法可以实现:
ImmutableDictionary<TKey, TValue>.Empty
更多信息可以在这里找到(最后几篇文章):https://github.com/dotnet/corefx/issues/25023
答案 4 :(得分:-1)
当键和值具有相同类型时(例如:string):
Enumerable.Empty<string>().ToDictionary(x=>x, x=>x)
答案 5 :(得分:-2)
{-# LANGUAGE RankNTypes, UnicodeSyntax #-}
import Control.Monad.Free