C# 每个键有多个值的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17341421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List with multiple values per key
提问by marcAntoine
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
我们如何创建具有多个值的列表,例如,list[0] 包含三个值 {"12","String","someValue"} Some 值与其他两个值相关联,我想使用列表而不是使用数组
string[, ,] read = new string[3, 3, 3];
采纳答案by Arran
Why not have a Listof Tuple's? It canbe unclear, but it will do what you are after:
为什么没有一个List的Tuple的?它可以是不清楚,但它会做你所追求的:
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 intendingto show, then we can give some ideas on how to make it much more readable.
尽管赋予这些值语义意义可能会更好。也许如果您让我们知道这些价值观打算展示什么,那么我们可以就如何使其更具可读性提出一些想法。
回答by Andrei
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;
回答by DonBoitnott
You could use Tuple:
你可以使用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:
MSDN:
回答by LTKD
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
class Entity{int i; String s; Object SomeValue;}然后就做
List<Entity> list=new List<Entity>()
List<Entity> list=new List<Entity>()
Alternatively you can use a matrix.
或者,您可以使用矩阵。
回答by MarcE
How about using a Lookup<TKey,TElement>?
使用一个Lookup<TKey,TElement>怎么样?
From MSDN:
来自 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.
查找类似于字典。区别在于字典将键映射到单个值,而查找将键映射到值的集合。

