locked
C# array of objects to array of strings RRS feed

  • Question

  • How can we convert the array of objects to array of strings?
    Friday, March 26, 2010 8:54 AM

Answers

  • object[] objs = ...;
    string[] strs = Array.ConvertAll(objs, o => o.ToString());

     


    Mattias, C# MVP
    • Proposed as answer by Ajey Godbole Friday, March 26, 2010 9:11 AM
    • Marked as answer by Harry Zhu Tuesday, March 30, 2010 6:05 AM
    Friday, March 26, 2010 9:06 AM

All replies

  • object[] objs = ...;
    string[] strs = Array.ConvertAll(objs, o => o.ToString());

     


    Mattias, C# MVP
    • Proposed as answer by Ajey Godbole Friday, March 26, 2010 9:11 AM
    • Marked as answer by Harry Zhu Tuesday, March 30, 2010 6:05 AM
    Friday, March 26, 2010 9:06 AM
  • Object[] objArray = new Object[5] { "First", "Second", "Third", "Fourth", "Fifth" };
    String[] stringArray = new String[objArray.Length];
    Array.Copy(objArray, stringArray, objArray.Length);
    Is this what you are looking for?
    Friday, March 26, 2010 9:08 AM
  • Mattias's solution is better
    • Marked as answer by AdityaReddyM Friday, March 26, 2010 9:47 AM
    • Unmarked as answer by Harry Zhu Tuesday, March 30, 2010 6:05 AM
    Friday, March 26, 2010 9:12 AM
  • i want to the each & every object nested data to be copied to the string array
    Friday, March 26, 2010 9:53 AM
  • i want each & every object's nested data to be copied to the string array
    Friday, March 26, 2010 9:54 AM
  • string[] strs = Array.ConvertAll(objs, o => o.ToString());

    btw, what is this "o" in the above code

    Friday, March 26, 2010 9:58 AM
  • o => o.ToString()

    is a lambda expression, a short way to define an anonymous method that takes an object refernece o as input and returns the string returned from o.ToString() as output. The C# compiler will take the lambda expression and initialize a Converter<TInput, TOutput> delegate that ConvertAll takes as parameter.

     

     


    Mattias, C# MVP
    Friday, March 26, 2010 11:18 AM
  • Sooooo... Mattias gave the answer, but Ajey's post was marked as the answer because it says that Mattias' is the right answer? lulwut? o.O
    Friday, March 26, 2010 11:24 AM
  • Here are a couple of similar ways as well though the one provided above is a perfect solution:

                Object[] o = new Object[] { new Object(), new Object() };
    
                String[] s1 = (from x in o select x.ToString()).ToArray();
                String[] s2 = o.Select(x => x.ToString()).ToArray();


    jon.stromer.galley
    Friday, March 26, 2010 2:39 PM
  • Sooooo... Mattias gave the answer, but Ajey's post was marked as the answer because it says that Mattias' is the right answer? lulwut? o.O


    LOL :)

    Hello Aditya,

    Please mark Mattias's post as Answer rather than mine.

    1. It will help others find the answer quickly. In this case, everyone will have to scroll up and down to figure out whats the solution
    2. As jgalley said in his post, Mattias solution is perfect :)

    Saturday, March 27, 2010 9:03 AM