【问题标题】:if i have a dictionary<string,list<int,int>>how can i access the key of this list in c#?如果我有一个字典<string,list<int,int>>如何在 c# 中访问此列表的键?
【发布时间】:2020-06-01 18:36:33
【问题描述】:

我想打印列表的所有键,即排序字典的值。

 SortedDictionary<string, object> dict2 = new SortedDictionary<string, object>();
 SortedList<int, int> innerdict2= new SortedList<int, int>();

foreach(var k in dict2)
{
    foreach(var item in k.value)
    {
        console.writeline(item);

    }
}

【问题讨论】:

  • 如果你有嵌套泛型,你真的应该使用类型别名。这种类型的阅读和写作只是一种不必要的痛苦。 |该类型也不存在。没有List&lt;K, V&gt;
  • @Christopher 但有一个SortedList&lt;K,V&gt;
  • @FranzGleichmann 那么标题是错误的,因为内部集合说list&lt;int,int&gt;。所以有太多的不确定性,甚至无法开始回答。
  • 那么您似乎想要Console.WriteLine(item.Key)
  • SortedDictionary&lt;string, object&gt; 那行是错误的。泛型是开发的,所以我们不再需要使用对象作为类型。有一堆非常好的、前泛型、值和可能的关键类型即对象集合。

标签: c# list dictionary


【解决方案1】:

下面的代码可以做到:

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

     // TODO: Fill in data

     foreach (var k in dict2)
     {
        foreach (var item in k.Value)
        {
           Console.WriteLine(item.Key);
        }
     }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2021-06-18
    相关资源
    最近更新 更多