Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Object by index C# [closed]

I have a foreach loop in C# which return some inventory data, the property location_id returns as an object[]. the loop as follows,

foreach (XmlRpcStruct item in result)
{
   object obj =  item["location_id"];
}

in debugger, I see the object as following, enter image description here

so I guess object is something like

obj[0] = 12
obj[1] = "WH/Stock"

I tried to access the obj like obj[0] then I get

Cannot apply indexing with [] to an expression of type 'object'

So, how can I access the object by index to retrieve the values such as 12 and WH/Stock

like image 346
nuwaus Avatar asked Dec 01 '16 07:12

nuwaus


2 Answers

Cast the obj as object[] using:

var list = (object[])obj;

Then you can use list[0].

like image 81
Emad Avatar answered Sep 25 '22 14:09

Emad


Specify object array type:

object[] obj =  item["location_id"];

Or, even simplier, let the compiler infer type:

var obj =  item["location_id"];
like image 41
grafgenerator Avatar answered Sep 24 '22 14:09

grafgenerator