-6

I want a List of OtherObjects all belonging to certain Objects. Is it possible to create a List like so?

List<Object, List<OtherObject>>

Or should I create a new class and do?

List<NewClass>

Or should I do something else overall? The size is dynamic so I didn't want to use an array.

How can I achieve this?

4

3 回答 3

4

你想要一个字典<>,例如

Dictionary<Object, List<OtherObject>>

Object关键在哪里List<OtherObject>,价值在哪里

于 2013-05-16T08:49:45.153 回答
1

你可以用它Tuple<Object, List<OtherObject>>来实现你想要的。

当您不想创建特殊类时,元组用于组合属性。Tuple<Object, AnotherObject, AnotherAnotherObject>如果需要,您可以使用。

您的代码将类似于:

List<Tuple<Object, List<OtherObject>>> list;

并使用它:

foreach(var tuple in list)
{
   var object = tuple.Item1;
   var innerList = tuple.Item2;
}
于 2013-05-16T08:53:35.223 回答
0

尝试使用

Dictionary<Object, List<OtherObject>>

希望我有所帮助

于 2013-05-16T08:53:13.470 回答