8

For logging purposes, I would like to call the .ToString() method of every object on an object[] array. How can I do this in the simplest way?

Say I have :

   myArray = new Object[]{"astring",1, Customer}

   Log(????);

How can I pass a string such as its value is equal to:

"astring".ToString()+1.ToString()+Customer.ToString()

Or better, with comma between each value.

Chris
  • 5,882
  • 2
  • 32
  • 57
Toto
  • 7,491
  • 18
  • 50
  • 72

4 Answers4

24

Like this:

Log(String.Join(", ", myArray.Select(o => o.ToString()).ToArray()));

Update:

From framework 4 the Join method can also take an IEnumerable<string>, so you don't need the ToArray:

Log(String.Join(", ", myArray.Select(o => o.ToString())));
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    @Jonathan: Correct. I always write it that way, so I don't know why I didn't this time... – Guffa Sep 03 '09 at 09:21
  • 1
    Jonathan, more what? If you were meant to use `String`, why did C# ship with the aliases at all? – Troels Thomsen Sep 03 '09 at 09:30
  • @troethom: The intention of the code is somewhat more clear if you use the alias string for the type, and the class String when you use static methods in the class. – Guffa Sep 03 '09 at 09:53
  • What is more clear? I truly believe it is bad practice to not be consistent. – Troels Thomsen Sep 03 '09 at 10:41
  • @troethom: You really don't see how it's clearer? If so, how can you argue against something that you don't understand? ;) – Guffa Sep 03 '09 at 11:25
  • Guffa, I really don't, but I would like to know why you think it is. If the argument is that lowercase "string" looks like an instance variable, the color of the keyword should tell you otherwise. Mixing "string"/"String" implies there is a difference (which there isn't). – Troels Thomsen Sep 04 '09 at 06:35
  • @troethom: There is a very distinct difference, and the difference is in the usage. You can use it as a data type to specify a reference for a string, and you can use it as a class name to access static members of the class. There is no reference used to access static members, so it's definitely two different usages. Using the class nade for static members makes this difference more obvious. – Guffa Sep 04 '09 at 08:22
  • 2
    BTW I think `string` is the MS standard; that is what StyleCop enforces. – satnhak Nov 05 '11 at 11:48
  • @BTyler: They use `String` in the documentation: http://msdn.microsoft.com/en-us/library/57a79xd0.aspx – Guffa Nov 05 '11 at 12:00
3

MoreLINQ has a ToDelimitedString method for precisely this purpose.

It uses a StringBuilder rather than using String.Join (from what I remember from previous questions, the efficiency of the two approaches depends heavily on what the input is) but it's simple enough. Here's the core code (there are a couple of wrappers to allow a default delimiter):

private static string ToDelimitedStringImpl<TSource>
    (IEnumerable<TSource> source, string delimiter)
{
    Debug.Assert(source != null);
    Debug.Assert(delimiter != null);

    var sb = new StringBuilder();

    foreach (var value in source)
    {
        if (sb.Length > 0) sb.Append(delimiter);
        sb.Append(value);
    }

    return sb.ToString();
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • No need to check sb.Length on each iteration. You can discard the 1st delimiter by returning sb.ToString(1, sb.Length-1) . – gimel Sep 03 '09 at 10:44
  • @gimel: It's slightly more complicated than that, to take account of long delimiters and empty sources. There are better ways though... I'm also not keen on the lack of braces here. Will fix when I get time :) – Jon Skeet Sep 03 '09 at 10:55
2

I regularly use...

String.Join(", ", Array.ConvertAll<object, string>(myArray, Convert.ToString))
Richard
  • 29,854
  • 11
  • 77
  • 120
1

A simple old fashion way :

string myString = "";
foreach(Object o in myArray)
    myString += o.ToString() + ", ";
// Remove the extra comma
if(myString.Length >=2)
    myString.Remove(myString.Length - 2);
Martin Delille
  • 11,360
  • 15
  • 65
  • 132