在這個例子中是否有更好的方法來模擬協方差?理想情況下我想這樣做:KeyValuePair協方差
private IDictionary<string, ICollection<string>> foos;
public IEnumerable<KeyValuePair<string, IEnumerable<string>> Foos
{
get
{
return foos;
}
}
但KeyValuePair<TKey, TValue>
是不協變的。
相反,我必須做的:
public IEnumerable<KeyValuePair<string, IEnumerable<string>>> Foos
{
get
{
return foos.Select(x =>
new KeyValuePair<string, IEnumerable<string>>(x.Key, x.Value));
}
}
有沒有更好/更清潔的方式?