簡體   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