views:

5248

answers:

7

ok I give up, how do you do this in one line?

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    //List<string> fields = values.ToList<string>();
    //List<string> fields = values as List<string>;
    //List<string> fields = (List<string>)values;

    List<string> fields = new List<string>();
    foreach (object value in values)
    {
        fields.Add(value.ToString());
    }

    //process the fields here knowning they are strings
    ...
}
+5  A: 

If you have LINQ available (in .NET 3.5) and C# 3.0 (for extension methods), then there is quite a nice one liner:

var list = values.Cast<string>().ToList();

You're not going get anything much shorter that what you've posted for .NET 2.0/C# 2.0.

Caveat: I just realised that your object[] isn't necessarily of type string. If that is in fact the case, go with Matt Hamilton's method, which does the job well. If the element of your array are in fact of type string, then my method will of course work.

Noldorin
great answer - much simpler than a linq select.
Scott Ivey
Note that this will fail if any value is not a string.
Mauricio Scheffer
@mausch: Yeah, I'd just edited the answer to add the caveat.
Noldorin
.Cast is very terse, thanks, nice to know, but in this case my objects as you noted in your caveat, are coming from a XAML Multibinding and could theoretically be any object and hence this would break if I sent a non-string
Edward Tanguay
@Edward Tanguay: Yeah, it now makes sense why you were doing that loop. No problem...
Noldorin
+20  A: 

Are you using C# 3.0 with LINQ? It's pretty easy then:

List<string> fields = values.Select(i => i.ToString()).ToList();
Matt Hamilton
I don't see anywhere that the object array was actually an array of strings. This answer is correct because ie duplicates *exactly* what the OP posted in a one-liner.
Andrew Hare
@Andrew: Yeah, good point. I clearly read the question too quickly. Caveat mentioned in my post now.
Noldorin
as you correctly assumed from the context, the objects are coming from XAML and theoretically could be of any type, so with this answer when I send an integer, it gets correctly converted to a string, perfect
Edward Tanguay
This is technically a two-liner if you count the using directive :D
Jim Schubert
A: 

While not a one liner with respect to List<> declaration, gives you same effect without requiring Linq.

List<string> list = new List<string>();
Array.ForEach(values, value => list.Add(value.ToString()));
Pat
A: 

Agree with Matt:

Noldorin's answer would fail if one object inside values can't be casted to a string, whereas ToString is defined on object.

So, Matt'sanswer is more correct.

odalet
+1  A: 

One more variant that might be correct:

List<string> list = values.OfType<string>().ToList();

This will filter out any objects in the original list that are not string objects, instead of either throwing an exception or trying to convert them all into strings.

Daniel Earwicker
.OfType is very interesting, will remember it, but in this context, I need to display the three values whatever they are (string, integer, etc.)
Edward Tanguay
+5  A: 

C# 2.0:

List<string> stringList = new List<string>(Array.ConvertAll<object,string>(values, new Converter<object,string>(Convert.ToString)));
Chris Persichetti
A: 
Array.ConvertAll(inputArray, p => p.ToString())

This converts an array of object type to array of string. You can convert to other type array by changing the lambda expression.

Suresh Deevi