Click here to Skip to main content
15,885,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have a scenario where i need to have a dictionary with a string key and associated two string values like the below.
The key is in bold.
I am using Visual studio 2008,framework 3.5. and c#.


dictionary<string,string,string>

What I have tried:

I tried the following approach:

1. dictionary<string,object>..........where the object encapsulated my two required fields.
But i would have to incur extra casting overhead while fetching the values.

2.Someone suggested me to use

dictionary<string,list><string>>

but i guess the list is itself a collection of objects and will also involve the casting overhead.


So guys please help me out....
is there any datastructure in c# that can store multiple values as valuetypes?


Thanks
Varun
Posted
Updated 16-May-22 6:56am
v2
Comments
Ravi Bhavnani 10-Apr-19 17:20pm    
> is there any datastructure in c# that can store multiple values as valuetypes?

Yes. See Tuple to store 1-8 values.
https://www.tutorialsteacher.com/csharp/csharp-tuple

/ravi

If you're lazy create a Dictionary of string and Tuple<string,string>.
C#
IDictionary<string, Tuple<string, string>> myDictionary = new Dictionary<string, Tuple<string, string>>();

If you're not lazy, create a class that holds your two strings (I'm going to call them Foo and Bar).
C#
public class MyOwnType {
   public string Foo { get; set; }
   public string Bar { get; set; }
   public MyOwnType(string foo, string bar) {
       Foo = foo;
       Bar = bar;
   }
}

C#
IDictionary<string, MyOwnType> myDictionary = new Dictionary<string, MyOwnType>();


Hope this helps,
Fredrik
 
Share this answer
 
Comments
Maciej Los 10-Apr-19 16:42pm    
Nice!
Depending on situation, you can use at least several approaches, such as:
1. Dictionary<TKey, List<TValue>>
2. Dictionary<TKey, List<ComplexObject>>
3. Dictionary<TKey, Tuple<TValue1, TValue2>>
etc.

Based on MSDN documention[^], you can create a dictionary object to create relationship between file extensions and application which is able to open that extensions. For example:

C#
Dictionary<string, List<string>> openWith = 
            new Dictionary<string, List<string>>();

        // Add some elements to the dictionary. There are no 
        // duplicate keys, but some of the values are duplicates.
        openWith.Add("notepad.exe", new List<string>(){".txt", ".vbs", ".csv"});
        openWith.Add("winword.exe", new List<string>(){".txt", ".vbs", ".csv", ".htm", ".html", ".doc", ".docx", ".dot", ".dotx", ".docm", ".dotm"});
        //and so on...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900