How to convert object[] to List<string> in one line of C# 3.0?

65,875

Solution 1

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

List<string> fields = values.Select(i => i.ToString()).ToList();

Solution 2

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.

Solution 3

C# 2.0:

List<string> stringList = new List<string>(Array.ConvertAll<object,string>(values, new Converter<object,string>(Convert.ToString)));

Solution 4

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.

Solution 5

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.

Share:
65,875
Angry Dan
Author by

Angry Dan

web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info

Updated on July 14, 2022

Comments

  • Angry Dan
    Angry Dan almost 2 years

    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
        ...
    }
    
    • Lucas
      Lucas almost 15 years
      Technically it's C# 3.0, which can be used to target .NET frameworks 2.0, 3.0, or 3.5 in VS2008. Confusing, I know :)
  • Scott Ivey
    Scott Ivey almost 15 years
    great answer - much simpler than a linq select.
  • Mauricio Scheffer
    Mauricio Scheffer almost 15 years
    Note that this will fail if any value is not a string.
  • Noldorin
    Noldorin almost 15 years
    @Andrew: Yeah, good point. I clearly read the question too quickly. Caveat mentioned in my post now.
  • Angry Dan
    Angry Dan almost 15 years
    .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
  • Angry Dan
    Angry Dan almost 15 years
    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
  • Noldorin
    Noldorin almost 15 years
    @Edward Tanguay: Yeah, it now makes sense why you were doing that loop. No problem...
  • Jim Schubert
    Jim Schubert almost 14 years
    This is technically a two-liner if you count the using directive :D