【问题标题】:Loop through a dictionary in C#循环遍历 C# 中的字典
【发布时间】:2019-03-02 14:50:46
【问题描述】:

我有以下代码,使用了接受状态和资本的类和字典,但我无法通过循环检索这些值。需要帮助是清除foreach 循环中发生的错误。

public class State {
  public string Capital { get; set; }
  public int Population { get; set; }
  public int Size { get; set; }

  public State(string aCapital, int aPopulation, int aSize) {
    Capital = aCapital;
    Population = aPopulation;
    Size = aSize;
  } // Constructor of the class State

  public static Dictionary<string, State> GetStates() {
    Dictionary<string, State> myStates = new Dictionary<string, State>(); // need the () because its a class

    // myStates takes 2 values, one is a string , that is a state and State ,  which is inturn takes 3 values - 
    // Capital,Population, size

    State addStateCapital = new State("Montgomery", 214141, 244);
    myStates.Add("Alabama", addStateCapital);
    // second set
    addStateCapital = new State("Sacramento", 214141, 244);
    myStates.Add("California", addStateCapital);

    return myStates;
  }
}

我的主程序如下..但我得到错误..

 var theState= State.GetStates();

 // this prints one item in a dictionary

 Console.WriteLine("State Capital of California is " +   theState["California"].Capital);

foreach (KeyValuePair<string,object> entry in theState)
{
    Console.WriteLine(entry.Key + " State Capital  is " +  entry.Value.ToString());
}

foreach 上的错误:

Cannot convert keyvaluePair &lt;string,DictionaryDemo.State&gt; to System... Generic KVP&lt;string,object&gt;

在了解如何正确检索值方面需要帮助。

【问题讨论】:

  • 它在错误信息中告诉你错误;您正在尝试将 KeyValuePair&lt;string, State&gt; 转换为 KeyValuePair&lt;string, object&gt;。我建议只使用foreach (var entry in theState)

标签: c# .net dictionary


【解决方案1】:

1) 在 KeyValuePair 中使用 State 而不是 Object。 由于您已经在 State.GetStates() 中指定了返回类型有

 Dictionary<string, State>

2) 还将 entry.Value.ToString() 更改为 entry.Value.Capital。因为它不会显示状态名称而是类对象名称。因此,将其更改为 entry.Value.Capital

foreach (KeyValuePair<string,State> entry in theState)
 {
 Console.WriteLine(entry.Key + " State Capital  is " +  entry.Value.Capital);
 }

【讨论】:

  • @VajraAyudha 告诉我这对你有什么好处。
【解决方案2】:

你的GetStates() 方法返回Dictionary&lt;string, State&gt;,所以在这一行

var theState= State.GetStates();

变量theState 分配给类型Dictionary&lt;string, State&gt;。但在下面的 foreach 块中,您尝试以 KeyValuePair&lt;string, object&gt; 的形式访问字典的值。

所以你必须像这样使用它:

 foreach (KeyValuePair<string, State> entry in theState)
 {
      Console.WriteLine(entry.Key + " State Capital  is " +  entry.Value.Capital);
 }

【讨论】:

    【解决方案3】:

    将代码 sn-p 更改为以下将解决您的问题

    foreach (KeyValuePair<string, State> entry in theState)
     {
          Console.WriteLine(entry.Key + " State Capital  is " +  entry.Value.Capital);
     }
    

    但要了解您收到该错误的原因,您可能需要仔细查看源自IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;Dictionary&lt;TKey, TValue&gt; 的定义。因此,当您使用 foreach 循环时,它在内部作用于 IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;。这里编译器期望 foreach 中的每个实体都是KeyValuePair&lt;TKey, TValue&gt; 类型。现在因为在字典中你有TValue 是状态类型,所以编译器需要KeyValuePair&lt;string, State&gt;。现在要深入了解这一点,您可能需要阅读“协变和逆变概念 c#”

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2022-01-11
      • 2023-03-12
      • 2022-09-22
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多