pat*_*lly 0 c# generics collections covariance
我正在设置一个包含某种集合类型值的Dictionary,如下所示:
IDictionary<string, ICollection<decimal>> goodPrices =
new Dictionary<string, List<decimal>>();
Run Code Online (Sandbox Code Playgroud)
但是,这行代码导致编译错误,无法将右侧隐式转换为左侧.显然,以下行不会产生错误:
IDictionary<string, List<decimal>> goodPrices =
new Dictionary<string, List<decimal>>();
Run Code Online (Sandbox Code Playgroud)
那么,在不暴露集合的底层实现的情况下,将"goodPrices"声明为一组字符串集合对的正确方法是什么?
那这个呢 :
IDictionary<string, ICollection<decimal>> goodPrices = new Dictionary<string, ICollection<decimal>>();
goodPrices.Add("myString", new List<decimal>() { /* my values */ });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |