nuwaus nuwaus - 7 days ago 8
C# Question

How to access Object by index C#

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

Answer

Cast the obj as object[] using:

var list = (object[])obj;

Then you can use list[0].