繁体   English   中英

C#元组字典

[英]C# Tuple Dictionary

我正在尝试扩展我对字典的使用,并希望我的对有多个值。

我在使用 one 将项目添加到字典时没有任何问题。 我曾尝试使用元组作为选项,但似乎无法弄清楚如何提取特定值。

这有效:

 Dictionary<string, string> voc4 = new Dictionary<string, string>();
 voc1.Add(key1, value);

 if (voc1.TryGetValue(result.Text.ToLower(), out string cmd))
 {
   ToSend(cmd);
 }

我试图用元组构建一个新字典:

Dictionary<string, Tuple<string ,string>> voc5 = new Dictionary<string,Tuple<string ,string>>();
voc5.Add(key1, new Tuple<string, string>(value,response));

if (voc5.TryGetValue(result.Text.ToLower(), out  string cmd))
 {//this is what I cant get working. I want to get the first value of thedictionary for 1 purpose and the other for a different purpose
 } 

如何根据键获取某些值并将它们用于不同的功能? 例子是:

 if (voc5.TryGetValue(result.Text.ToLower(), out  string cmd))// the first value
 {
   ToSend(value 1 from tuple).ToString();

   ToDisplay(value 2 from tuple).ToString();     
 } 

任何帮助都会很棒

您只能从字典中获取整个Tuple<string, string>实例。 即使您只想使用其中一个值,您也必须取出整个元组。

检索元组后,使用ItemN访问元素。

if (voc5.TryGetValue(result.Text.ToLower(), out Tuple<string, string> cmd))
{
    ToSend(cmd.Item1).ToString();

    ToDisplay(cmd.Item2).ToString();   
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM