4

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;

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
Usama Mohsin
  • 79
  • 1
  • 2
  • 11
  • 1
    create List – Seb Feb 04 '16 at 18:23
  • 5
    What are you **actually** trying to do? – cbr Feb 04 '16 at 18:24
  • Suppose you _could_ do `List list;` What would the compile-time type of `list[0]` be? You need a common base type = in this case, `object`. – D Stanley Feb 04 '16 at 18:27
  • 2
    It seems likely that there's a better approach to your problem. You should describe exactly what you're trying to accomplish. – ic3man7019 Feb 04 '16 at 18:29
  • I am developing a software for imputation that will impute categorical and numerical values so I was thinking to use same list to store different data types so that i don't have to parse values again and again. @cubrr – Usama Mohsin Feb 05 '16 at 14:06

6 Answers6

10

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.

David
  • 208,112
  • 36
  • 198
  • 279
7

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.

Usman lqbal
  • 935
  • 1
  • 9
  • 31
5

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.

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);
Swen
  • 124
  • 5
1

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>().

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...

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
1

Best way to solve this is to use Interface you can find solution here list of multiple data types

Community
  • 1
  • 1
Babajide Apata
  • 671
  • 6
  • 18
  • 1
    While it is good suggestion (probably better be duplicate suggestion/comment rather than answer by the way), it is not helpful for OP as there is no shared interface between `int`, `double` and `string`. – Alexei Levenkov Feb 04 '16 at 18:41
1

You can use ArrayList - the old non generic list. THis allows you to put anything in it, int, string, FooObject, bool,....

pm100
  • 48,078
  • 23
  • 82
  • 145