3

I have seen how to cast an object to a string array using the code below,

string[] arr = ((IEnumerable)paraArray).Cast<object>()
                             .Select(x => x.ToString())
                             .ToArray();

My question though is if paraArray is a object (but the data is a multidimensional array) how can I cast it to a multidimensional string array?

3
  • That code isn't casting anything as a string array. It's converting each object in a list into a string and then creating a new string array and populating it with those values. You seem to be under some serious misconceptions regarding what casting actually is and does. Casting something as a string array would look like this: var myArray = (string[]) myObject;. Commented Aug 11, 2016 at 7:20
  • The data is a multidimensional array of what? Object? Commented Aug 11, 2016 at 7:21
  • yeh the paraArray is passed to the function as an object. The object though I know contains rows and columns of string and double data Commented Aug 11, 2016 at 7:22

2 Answers 2

5

You can't achieve this via ToArray. The best, IMHO, you can do is

  object[,] paraArray = new object[,] {
    {1, 2, 3 },
    {4, 5, 6 },
  };

  ...

  string[,] arr = new string[paraArray.GetLength(0), paraArray.GetLength(1)];

  for (int i = 0; i < arr.GetLength(0); ++i)
    for (int j = 0; j < arr.GetLength(1); ++j)
      arr[i, j] = paraArray[i, j].ToString();

2d arrays are not very convenient when working with Linq, that's why often jagged arrays (array of array) are preferable:

  object[][] paraArray = new object[][] {
    new object[] {1, 2, 3 },
    new object[] {4, 5, 6 },
  };

  ... 

  // Working with jagged array is much easier than with 2d one 
  string[][] arr = paraArray
    .Select(line => line
       .Select(item => item.ToString())
       .ToArray())
    .ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

Oh its fine for it to be a jagged array. I will give that a go. If my paraArray is being passed as an object would I able to use the GetLength method or I guess I would have to do something to paraArray first?
Object doesnt natively inherit a length method, you will have to cast it
@downrep_nation: I call arr.GetLength(...) on arr which is an array, not just object
@DmitryBychenko thats fine, i was answering op's question
3

Instead of using LINQ for this, you can use Array.Copy, as in this answer.

object[,] paraArray = new object[2, 2] { { "a", "b" }, { "c", "d" } };

string[,] stringArray = new string[paraArray.GetLength(0), paraArray.GetLength(1)];

for (int i = 0; i < paraArray.GetLength(0); ++i)
{
    Array.Copy(paraArray, i * paraArray.GetLength(1), stringArray, i * stringArray.GetLength(1), paraArray.GetLength(1));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.