List with multiple values ​​per key

how can we create a list with several values, for example, list [0] contains three values ​​{"12", "String", "someValue"} Some value is associated with two other values ​​that I want to use instead of using an array

string[, ,] read = new string[3, 3, 3]; 
+4
source share
5 answers

Why not have a List of Tuple ? It may not be clear, but it will do what you need:

 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 meaning to these meanings. Perhaps if you tell us what the values ​​intend to show, then we can give some ideas on how to make it more readable.

+11
source

Use list of lists:

 List<List<string>> read; 

Or, if you want a key-to-set relationship, use a list dictionary:

 Dictionary<string, List<string>> read; 
+6
source

How about using Lookup<TKey,TElement> ?

From MSDN:

Search is like a dictionary. the difference is that the dictionary matches keys to single values, whereas Lookup matches keys to collections of values.

+2
source

You can use Tuple :

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

MSDN:

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

+1
source

Or you can create a separate class that will contain all the values. Let me name this Entity class:

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

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

Alternatively, you can use a matrix.

0
source

Source: https://habr.com/ru/post/1488473/


All Articles