Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

or a List<> with multiple data types like List<int,double,string> list;

like image 741
Usama Mohsin Avatar asked Nov 28 '22 06:11

Usama Mohsin


2 Answers

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.

like image 79
David Avatar answered Dec 05 '22 13:12

David


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.

like image 41
Usman lqbal Avatar answered Dec 05 '22 15:12

Usman lqbal