Pregunta

I am using a line like this:

Array stringArray = Array.CreateInstance(typeof(String), 2, 2);

to consume a method that returns a 2D System.Array with my stringArray variable. However stringArray = MyClassInstance.Work(...,... ...); is not filled with the return array of that function.

How should I define my collection variable so that it can be assigned to the return value of a method which returns a 2D System.Array string array value?

Thank you

¿Fue útil?

Solución

Array.CreateInstance returns an Array, without its dimension and length specified(but anyway it's an System.Array which is the base class of all arrays in C#). In most circumstances a return array from a method is more like:

public string[] MyMethod()
{
}
public int[,] Another(int something)
{
}

string[] theStringResult = MyMethod();
int[,] theIntResult = Another(1);

Otros consejos

On your 2D-Array:

Array.CreateInstance(typeof(String), 2, 2);

This returns a string[,] cast to Array. It's identical to (Array)(new string[2,2]). So you can cast it to string[,] if you want.

But why do you want to use that anyways? Can't you just use a string[,] instead? Don't you know that the element type is string at compile-time? Perhaps make in a generic parameter.

On your overserved problem:

However stringArray = MyClassInstance.Work(...,... ...); is not filled with the return array of that function.

We can't help you with that part of the problem unless you post the code where it happens. It seems like the MyClassInstance.Work doesn't return the array instance you created. But unless you show the code with the bug I can't tell you where it is.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top