简体   繁体   English

列出<>多个列表c#

[英]Lists<> with multiple lists c#

I am searching for a solution to create List<> of Lists with different data types like List<List<int>,List<double>,List<string>> list; 我正在寻找一个解决方案来创建List<>列表,其中包含不同的数据类型,如List<List<int>,List<double>,List<string>> list;

or a List<> with multiple data types like List<int,double,string> list; 或具有多种数据类型的List<> ,如List<int,double,string> list;

In all seriousness... why? 严肃地说...... 为什么?

The code which consumes these structures is going to be confusing and difficult to support. 消耗这些结构的代码将变得令人困惑并且难以支持。 It really, really will. 它真的,真的会。

If you have a custom structure to your data, create a custom object to represent that structure. 如果您的数据具有自定义结构,请创建一个自定义对象来表示该结构。 Something like this: 像这样的东西:

class MyThing
{
    public int Widget { get; set; }
    public double Foo { get; set; }
    public string Something { get; set; }
}

Use actual meaningful names for your values/types/etc. 为您的值/类型/等使用实际有意义的名称。 of course, because that's the entire point of doing this. 当然,因为这是完成这一切的重点。

Then just have a list of those: 然后只列出一个:

var things = new List<MyThing>();

As your structure continues to change and grow, you build the logic of that structure into the object itself , rather than into all of the consuming code which uses it. 随着您的结构不断变化和增长,您将该结构的逻辑构建到对象本身中 ,而不是构建使用它的所有消费代码中。 The code which uses it then more closely approximates the semantics of the operations it's trying to perform, rather than a dizzying assortment of syntactic operations. 然后使用它的代码更接近于它试图执行的操作的语义,而不是令人眼花缭乱的语法操作。

You can make a list of lists by 您可以按列表制作列表

List<List<int>>

To have a list with multiple data types you could use a Tuple which can take up to 8 items. 要拥有包含多种数据类型的列表,您可以使用最多可以包含8个项目的元组

List<Tuple<string, int>>
List<Tuple<string, string, int>>

You could even go crazy and do the following 你甚至可以发疯并做以下事情

Tuple<List<int>, int, int> tuple = new Tuple<List<int>, int, int>(new List<int>(), 2, 3);

May be you can do like this 也许你可以这样做

 public class Helper
    {
        public object value;
        private string Type;
    }

then create list 然后创建列表

List<Helper> myList=new List<Helper>();

use like this 像这样用

myList.Add(new Helper {value = 45,Type = "int"});
myList.Add(new Helper {value = 45,Type = "int"});
myList.Add(new Helper {value = "hello",Type = "string"});

and the covert according to Type . 和根据类型的隐蔽。

If I'm understanding you correctly, I think you need a little helper class: 如果我正确地理解你,我认为你需要一个小帮手类:

public class MyInfo {
    public int MyInt32 { get; set; }
    public double MyDouble { get; set; }
    public string MyString { get; set; }
}

Then, make a list of those: var myList = new List<MyInfo>() . 然后,列出以下内容: var myList = new List<MyInfo>()

You can also use a List<Tuple<int, double, string>> , but I would recommend creating your own class if the scope of usage is wider than a single class, because Tuples properties aren't very descriptive as to what they are: Item1 , Item2 , Item3 ... 你也可以使用List<Tuple<int, double, string>> ,但如果使用范围比单个类宽,我建议你创建自己的类,因为Tuples属性对它们的含义不是很具描述性。 : Item1Item2Item3 ......

解决此问题的最佳方法是使用Interface,您可以在此处找到多种数据类型的解决方案列表

You can use ArrayList - the old non generic list. 您可以使用ArrayList - 旧的非通用列表。 THis allows you to put anything in it, int, string, FooObject, bool,.... 这允许你把任何东西放进去,int,string,FooObject,bool,....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM