2

I need something resembling the following:

Dictionary<string, List<int>, List<DateTime>> = new Dictionary<string, List<int>, List<DateTime>>() 

are there any built in class in C# which offer something like the above?

Edit: for people who can't see why anything like this could ever possibly be useful...

Imagine if you could write something like this:

mySuperDictionary SuperDictionary<string, List<int>X, List<int>Y> ..... myXvalues = mySuperDictionary["myKey"].X; myYvalues = mySuperDictionary["myKey"].Y; 

personally I think that would be a pretty neat.

3
  • 7
    how about putting the int and the DateTime in a struct/class? Commented Jan 5, 2010 at 17:33
  • How exactly is this meant to be used? I cannot even theorize a situation where this would be useful. Perhaps you want to lookup an int and datetime for each string.. yes? Commented Jan 5, 2010 at 17:34
  • Posting the code you want to compile is not very helpful. Tell us what behavior you want out of such a data structure. Commented Jan 5, 2010 at 17:39

7 Answers 7

26

No.

Create a Pair or Tuple type yourself.

Something like:

class Pair<T,V> { T First{get; set;} V Second{get; set;} } 

Then you can declare a Dictionary<string, Pair<List<int>, List<DateTime>>.

Sign up to request clarification or add additional context in comments.

2 Comments

Its worth noting that in .NET 4.0 there is/will be a Tuple type. So this answer will be out of date shortly for those of us on the cutting edge.
9

I do not believe so. I think it would be better if you made a custom object to hold your List<int> and List<DateTime> objects.

Dictionary<string, CustomClass>> = new Dictionary<string, CustomClass>(); public class CustomClass { public List<int> IntegerList { get; set; } public List<DateTime> DateTimeList { get; set; } } 

Comments

5

Normally, you'd just do:

Dictionary<string, KeyValuePair<List<int>,List<DateTime>>> dictionary; 

A custom class is usaully nicer, however, for this type of thing. Having Dictionary<Key, Value, Value> doesn't really add any value - it's still a single key -> something lookup, so just make your value handle it.

Comments

0

There are not built in class as your requirement but you can create your own.

Comments

0

No, you'll have to roll your own. This could be easily done with a simple struct or class. This answer has good information on how to do something like this using tuples.

Comments

0

I think there is a generic Pair<> class in the framework, which you could use to associate two "values" with each other.

3 Comments

There is KeyValuePair2` in .NET 2.0, but it is ugly because it has a very long name and it has other meanings associated with it. In .NET 4.0 there's the series of Tuple classes.
Duly noted. I've used the KeyValuePair previously; it is a bit clunky, and has a certain implied meaning, besides just "Pair". I'll see what I can find about the Tuple you mentioned.
rolling your own Tuple is trivial.
0

Strongly typed datatable could produce the same data (and structure) you need. See below.

http://www.codeproject.com/KB/database/TypedDataTable.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.