views:

573

answers:

4

Hi,

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

Say I have :

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

Log(????);

How can I pass un string such as its value is equals to "astring".ToString()+1.ToString()+Customer.ToString()

Or better, with coma beetween each value.

+8  A: 

Like this:

Log(String.Join(", ", myArray.Select(o => o.ToString()).ToArray()));
Guffa
string should be capital String. It's more ....
Jonathan Shepherd
@Jonathan: Correct. I always write it that way, so I don't know why I didn't this time...
Guffa
Jonathan, more what? If you were meant to use `String`, why did C# ship with the aliases at all?
troethom
@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
What is more clear? I truly believe it is bad practice to not be consistent.
troethom
@troethom: You really don't see how it's clearer? If so, how can you argue against something that you don't understand? ;)
Guffa
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).
troethom
@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
+1  A: 

I regularly use...

String.Join(", ", Array.ConvertAll<object, string>(myArray, Convert.ToString))
Richard
+2  A: 

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
No need to check sb.Length on each iteration. You can discard the 1st delimiter by returning sb.ToString(1, sb.Length-1) .
gimel
@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
+1  A: 

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);
tinmaru