marcAntoine
marcAntoine

Reputation: 678

List with multiple values per key

how do we create a list with multiple values for example , list[0] contains three values {"12","String","someValue"} the Some value is associated to the two other values i want to use a list rather than using an array

string[, ,] read = new string[3, 3, 3];

Upvotes: 7

Views: 52165

Answers (5)

MarcE
MarcE

Reputation: 3731

How about using a Lookup<TKey,TElement>?

From MSDN:

A Lookup resembles a Dictionary. The difference is that a Dictionary maps keys to single values, whereas a Lookup maps keys to collections of values.

Upvotes: 3

Arran
Arran

Reputation: 25056

Why not have a List of Tuple's? It can be unclear, but it will do what you are after:

var list = new List<Tuple<string, string, string>>();
list.Add(new Tuple<string, string, string>("12", "something", "something"));

Although it would probably be better to give these values semantic meaning. Perhaps if you let us know what the values are intending to show, then we can give some ideas on how to make it much more readable.

Upvotes: 17

LTKD
LTKD

Reputation: 214

Or you can make a separate class that will contain all the values. Let's call this class Entity:

class Entity{int i; String s; Object SomeValue;} and then just do

List<Entity> list=new List<Entity>()

Alternatively you can use a matrix.

Upvotes: 0

DonBoitnott
DonBoitnott

Reputation: 11025

You could use Tuple:

List<Tuple<String, String, String>> listOfTuples = new List<Tuple<String, String, String>>();
listOfTuples.Add(new Tuple<String, String, String>("12", "String", "someValue"));

The MSDN:

http://msdn.microsoft.com/en-us/library/dd383822.aspx

Upvotes: 3

Andrei
Andrei

Reputation: 56688

Use list of lists:

List<List<string>> read;

Or if you want key-multiple values relation, use dictionary of lists:

Dictionary<string, List<string>> read;

Upvotes: 6

Related Questions