【问题标题】:How to Get Keys and Value In Array Object [duplicate]如何在数组对象中获取键和值 [重复]
【发布时间】:2017-10-11 05:22:25
【问题描述】:

我想从数组对象中获取键和值

【问题讨论】:

  • 为什么要使用ArrayLists?
  • 支持net 2.0
  • 为什么还要使用.net 2.0? List 似乎在那里。
  • 欢迎来到 Stack Overflow! Please don't post your code as an image.
  • st[u]是什么类型?

标签: c#


【解决方案1】:

您可以尝试使用KeyValuePair 从数组中获取键和值。

foreach(KeyValuePair<string, string> kvp in m)
{
 Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);

}

【讨论】:

  • 你少了一个空格。
  • @someone 但在 OP 图像中有数组列表。不赶时间?
  • 是的,这就是我修正评论的原因。
【解决方案2】:

你可以这样做:

foreach(KeyValuePair<string, string> kv in st[u])
{ 
   string key = kv.Key;
   string val = kv.Value;
   //Business logic here
}

【讨论】:

  • 我很确定这是我的错。
【解决方案3】:
    Hashtable hashtable = new Hashtable();
    hashtable[1] = "One";
    hashtable[2] = "Two";
    hashtable[13] = "Thirteen";

    foreach (DictionaryEntry entry in hashtable)
    {
        Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
    }
You can use hashtable like this

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    相关资源
    最近更新 更多