Convert string[] to Array

A string[] is already an Array (because each concrete array type derived from Array) – and Split already gives a string[], so you don’t need to call ToArray on the result.

So this:

data = (Array) ids.Split(',').ToArray();

can just be:

data = ids.Split(',');

The result is an array, so there’s no other work to do.

Note that changing the value of data will not change the caller’s array at all, if that’s what’s going wrong. You should read my article on parameter passing to find out more about that side of things.

EDIT: Now that we can see your code, I believe the problem has very little to do with arrays. It’s this:

String q = string.Format(query, data.GetValue(i).ToString()); // Fails

I strongly suspect that your query contains something like {1} which is trying to format using an argument which isn’t present.

It should be easy to validate this – just separate out the parts:

String value = data.GetValue(i).ToString();
String q = string.Format(query, value);

Leave a Comment