【问题标题】:c# dictionary calling value with key problem [duplicate]c#字典调用值与关键问题[重复]
【发布时间】:2019-03-12 15:30:19
【问题描述】:
Dictionary fruit<string,string> = new Dictionary<string,string>();
fruit.Add("name","orange");
fruit.Add("vitamin","C");

我想用键调用值。 但是,如果我写 水果.Key, c# 给出“名称” 什么问题?

【问题讨论】:

  • 你想获得价值吗?如果是这样,很容易只需 fruit["KEY"] 应该给你价值!
  • 请添加更多关于您的问题的详细信息。这段代码甚至无法编译。

标签: c# dictionary key key-value


【解决方案1】:

两者都可以:

string name = fruit["name"];

或:

string vit = fruit.Single(c => c.Key == "vitamin").Value;

【讨论】:

    【解决方案2】:

    问题就在这里(来自我的工作台):

    WhatisMethodForGetValue()  {
    //????????????????????????????
    }
    
    
    
      var tas = new Dictionary<string, string>();
    
          string sayi;
                string renk;
                bool okey = false;
    
                        tas["sayi"] = seriler[j];
                        tas["renk"] = renkler[i];
                        tas["okey"] = okey.ToString();
    
    
    
      foreach (KeyValuePair<string, string> mytas in (dynamic)TAS[v])
                    {
    
    
    Button Tas = new Button();
                        Tas.BackColor = Color.White;
    Tas.Text = WhatisMethodForGetValue(mytas["sayi"]);
                }
    

    【讨论】:

    • 这似乎不是一个答案。也许您打算编辑您的问题?
    猜你喜欢
    • 2021-07-22
    • 1970-01-01
    • 2012-11-11
    • 2021-09-16
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2021-08-02
    • 2020-05-12
    相关资源
    最近更新 更多