Search code examples
c#.netstringsystem.array

How to consume a method that returns a 2d System.Array (not string[,]) in C#?


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


Solution

  • 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);