首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#:SortedList<TKey,TValue>实现了IReadOnlyDictionary<TKey,TValue>吗?

C#:SortedList<TKey,TValue>实现了IReadOnlyDictionary<TKey,TValue>吗?
EN

Stack Overflow用户
提问于 2018-05-11 22:09:06
回答 1查看 160关注 0票数 0

根据SortedListSortedDictionary的文档,它们都实现了IReadOnlyCollection<KeyValuePair<TKey,TValue>>IReadOnlyDictionary<TKey,TValue>。.net 4.5.2的文档版本也是如此,所以我的项目,针对C# 6.0 (维基百科说是.NET 4.6 ),应该包括在内。

根据VS 2015的编译器,他们没有。

我不能将一个SortedDictionary<int, string>传递给这个函数:

代码语言:javascript
复制
private static void funcIROCollKvp1 (IReadOnlyCollection<KeyValuePair<int, string>> i_rocollkvp)

哪一个是错的?

下面是完整的代码:

代码语言:javascript
复制
private static void funcIROCollKvp1 (IReadOnlyCollection<KeyValuePair<int, string>> i_rocollkvp)
{
}

private static void Main ()
{
  SortedList<int, string> listkvp = new SortedList<int, string> ();
  SortedDictionary<int, string> dict = new SortedDictionary<int, string> ();

  funcIROCollKvp1 (listkvp);
  funcIROCollKvp1 (dict);
}

参数1:无法从“Projects\testTKS_vs2015\test03\Program.cs(18,24,18,31):”转换为“'System.Collections.Generic.IReadOnlyCollection>‘”

参数1:无法从“Projects\testTKS_vs2015\test03\Program.cs(19,24,19,28):”转换为“'System.Collections.Generic.IReadOnlyCollection>‘”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-11 23:47:20

它并不是说你不能通过它们,它只是说它不知道如何通过。您可以手动将对象强制转换为IReadOnlyCollection。

代码语言:javascript
复制
        funcIROCollKvp1((IReadOnlyCollection < KeyValuePair<int, string> > )listkvp);
        funcIROCollKvp1((IReadOnlyCollection < KeyValuePair<int, string> > )dict);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50300881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档