I feel stupid for asking, but there must be a one liner that does the equivalent or near equivalent of the code below in c#... so can you tell me what it is?
public static string[] ToStringArray(int[] i)
{
if (i==null) return null;
string[] result = new string[i.Length];
for (int n= 0; n< result.Length; n++)
result[n] = i[n].ToString();
return result;
}
How about an extension method?
public static string[] ToStringArray<T>(this IEnumerable<T> items)
{
return items.Select(i => i.ToString()).ToArray();
}
Using LINQ:
int[] ints = { 1, 2, 3 };
string[] strings = ints.Select(i => i.ToString()).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With