Search code examples
c#arrayslistobject

how get item from list which is in Array of Object in c#


This is my code, I can`t get elements from the list which is situated Object array. How I can get "info" from data?

Object[] data = {
new List<string>() {"name", "name2", "name3", "name4",},
new List<string>() { "info", "info2", "info3", "info4",},
new List<int>() { 2002, 1999, 2005, 1980 },
new List<int>() { 12000, 1000000, 2500, 900000 },
new List<int>() { 1337, 2828, 1890, 4210 },
};

I tried to get elements with the help:

var i = data[1][1]
var i = data[1,1]

Solution

  • data[1] is not a list, it's an object, technically it is object?. so you cant use the index on it.

    you can get the value like this.

    var ls = data[1] as List<string>;
    
    var item = ls[1];
    

    or var item = ((List<string>)data[1])[1];

    Edit: both cases are compiling for me.

    enter image description here