tags:

views:

313

answers:

7

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.

+9  A: 

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; }
}
Brandon
damn, beat me to it :)
Scott Anderson
+25  A: 

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>>.

Kevin Montrose
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.
Kevin Montrose
http://stackoverflow.com/questions/152019/will-a-future-version-of-net-support-tuples-in-c/1047961#1047961
Andreas Grech
A: 

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

Ravia
+5  A: 

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.

Reed Copsey
A: 

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.

Nick
A: 

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

Grant Palin
There is `KeyValuePair`2` 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.
Martinho Fernandes
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.
Grant Palin
@Grant Palin, rolling your own Tuple is trivial.
Benjol
A: 

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

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

cethie