Tutorial :How do I build a generic C# array content printer that accepts both objects and primitives?



Question:

I'm just trying to quickly debug a webservice by printing out the contents of an int and string array. Is there anyway to do this in one method?

I tried

public static string ArrayToString(object[] array)  {      StringBuilder sb = new StringBuilder();        foreach (Object item in array)      {          sb.Append(item.ToString());          sb.Append(" ");      }        return sb.ToString();  }  

But this chokes when I try and send it a primative array (int[]) due to it not being an Object.

Which I find strange as sending an int to the method below works

static void PrintObject(object obj)  {      Console.WriteLine(obj.ToString());  }  

Is there anyway to do this in one method or am I just going to have to man up and create two separate methods?


Of course whether I should be using arrays at all is questionable, but the web service is already in place.

See also:


Solution:1

public static string ArrayToString<T>(T[] array)  {     StringBuilder sb = new StringBuilder();        foreach (T item in array)      {          sb.Append(item.ToString());          sb.Append(" ");      }        return sb.ToString();  }  

Then

int[] x = new int[] { 1, 2, 3 };    string[] y = new string[] { "a", "b", "c" };    Console.WriteLine(ArrayToString(x));  Console.WriteLine(ArrayToString(y));  


Solution:2

Try something like this:

static String toString<T>(IEnumerable<T> list)  {      StringBuilder sb = new StringBuilder();        foreach (T item in list)      {          sb.Append(item.ToString());          sb.Append(" ");      }        return sb.ToString();  }  

The compiler will happily infer the type of T by the type of the list you pass in like this:

toString(new int[] { 1, 2, 3 });  toString(new String[] { "1", "2", "3" });  


Solution:3

Why did you choose an array? Your code depends on none of the specific properties of an array and instead just sees a list of objects. It seems like IEnumerable would be a much better choice and would allow for virtually any collection of objects

public static string ArrayToString(IEnumerable source)  {      StringBuilder sb = new StringBuilder();        foreach (Object item in source)      {          sb.Append(item.ToString());          sb.Append(" ");      }        return sb.ToString();  }  


Solution:4

private string PrintObjects<T>(IEnumerable<T> objs)  {      return string.Join(" ", objs.Select(o => o.ToString()).ToArray());  }  


Solution:5

There are a few ways to do this.

If you have Linq, you could do

int[] foo = new int[] {1,2,3,4};  ArrayToString(foo.Cast<Object>().ToArray());  

and then send it.

Or, you could make a generic extension method

public static string ToString<T>(this IEnumerable<T> val)  {      StringBuilder sb = new StringBuilder();      foreach(var item in val)       {         sb.Append(item.ToString());         sb.Append(" ");      }      return sb.ToString();  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »