简体   繁体   中英

C# Tuple Dictionary

I am trying to expand my usage of the dictionary and want to have multiple values for my pairs.

I have had no issues with adding items to the dictionary with one . I have tried to use Tuple as an option but cant seem to figure out how to extract a particular value.

this works:

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

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

I tried to build a new dictionary with Tuple:

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
 } 

How can I get certain values based on the key and use them for different functions? example would be:

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

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

Any help would be great

You can only get the entire Tuple<string, string> instance out of your dictionary. Even if you'd only like to use one of the values, you have to fetch the entire tuple out.

After you retrieve the tuple use ItemN to access the elements.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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