fearofawhackplanet
fearofawhackplanet

Reputation: 53446

is there a <key, value, value> dictionaryish type in C#?

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.

Upvotes: 2

Views: 459

Answers (7)

cethie
cethie

Reputation: 49

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

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

Upvotes: 0

Grant Palin
Grant Palin

Reputation: 4558

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

Upvotes: 0

Nick
Nick

Reputation: 1718

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.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564891

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.

Upvotes: 5

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9950

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

Upvotes: 0

Kevin Montrose
Kevin Montrose

Reputation: 22611

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

Upvotes: 26

Brandon
Brandon

Reputation: 70052

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; }
}

Upvotes: 9

Related Questions