I'm trying to get a feel for the syntax of C# and I want to print the elements of an array that's stored inside an element of different array.
int[] numarray = new int[3];
numarray[0] = 5;
numarray[1] = 6;
numarray[2] = 6;
object[] array = new object[6];
array[0] = 1;
array[1] = "string";
array[2] = "test";
array[3] = numarray;
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[3].Length); // .Length is an error?
//how do I print the values of array[3][i]?
}
arrayis of typeobject. Therefore each element in the array is also of typeobject, and the typeobjectdoesn't have aLengthproperty.objecttype. Usually it should be a more specific type and/or means you're doing something "nasty" (like here).objecttoint[]. UseConsole.WriteLine(((int[])array[3]).Length);However, you're generally better off embracing strong-typing and coding in a way where you don't have to up-cast any more than you absolutely have to.