C# 从字典中检索信息

C# 从字典中检索信息,c#,winforms,C#,Winforms,我已将信息保存在词典中。以及由用户更新的中的词典。如何从字典中检索信息。我已经完成了一些代码。我不知道如何从字典里得到信息 //Create the dictionaries Dictionary<int, int> waytosave = new Dictionary<int, int>(); Dictionary<int ,int> numberControls = new Dictionary<int,int>

我已将信息保存在词典中。以及由用户更新的中的词典。如何从字典中检索信息。我已经完成了一些代码。我不知道如何从字典里得到信息

     //Create the dictionaries 
     Dictionary<int, int> waytosave = new Dictionary<int, int>();
     Dictionary<int ,int> numberControls = new Dictionary<int,int>();

          private void btnRun_Click(object sender, EventArgs e) 
       { 
    ///Setting up the coordinates
       int xCoor; 
       int yCoor;
       Random coor = new Random();
       int value =7; 
       for (int x = 0; x < value; x++)
          { 
        //Creating Random NumeircalUpdown. 
       //Using those the user can change the values.
        NumericUpDown numiNumber = new NumericUpDown();   
        xCoor = coor.Next(0, 500);     
       yCoor = coor.Next(0, 500);              
       numiNumber.Name = x.ToString();       
       numiNumber.Location = new Point(xCoor, yCoor);  
       numiNumber.Size = new System.Drawing.Size(50, 15);    
        numiNumber.Maximum = 100;    
        numiNumber.Minimum = 0; 
      //Saveing the numericalUpdowns   
        numberControls.Add(x, 0); 
       this.pnlNodes.Controls.Add(numiNumber); 

      //Make it respond to the clicking event    
         numiNumber.Click += new EventHandler(GetNumUpDownValue); 
         } 
         }
      //Get the values for the NumericUpDown
    public void GetNumUpDownValue(object sender, EventArgs e)
     { 
     int iname = int.Parse(((NumericUpDown)sender).Name);
     int ivalue = (int)((NumericUpDown)sender).Value;
    //check and update the list 
     if (waytosave.ContainsKey(iname))
       {
        waytosave[iname] = ivalue;
       }
           else
           { 
          waytosave.Add(iname, ivalue);
               } 

            txtOutputs.Text += "\r\r\n" + "   Node # " + iname + " = " + waytosave[iname].ToString(); 
             }

             private void btnRoundRobin_Click(object sender, EventArgs e)         {
              //how can i get the saved information from the waytosave dictionary
       //Can you advise me please????. 
         }
//创建字典
Dictionary waytosave=新字典();
字典编号控件=新建字典();
私有void btnRun\u单击(对象发送方,事件参数e)
{ 
///设置坐标
int xCoor;
int yCoor;
Random coor=新的Random();
int值=7;
对于(int x=0;x
这里有一种方法,假设您并不真正关心键的顺序

// Untested Code

int index = -1;
private void btnRoundRobin_Click(object sender, EventArgs e)
{
    var keys = waytosave.Keys;
    if(keys.Count == 0) return;
    index = (index + 1) % keys.Count;

    int key = keys[index];
    int value = waytosave[key];
}

您需要检索哪些信息?iname和ivalue?我正在考虑另外两个数组,一个用于iname,一个用于ivalue。要从
字典中获取值,您可以使用与设置值相同的索引器语法:
int data=waytosave[key]也许您应该使用元组,它在语义上似乎更适合您的需要。否则,您可以通过字典循环获取所有值,如
foreach(KeyValuePair kv in waytosave)
kv.Key在名称中,以及kv.Value ivalue.it只是给我名称而不是值。实际上,它只是返回索引而不是字典中的值。