score:7

Accepted answer

You can use Select() to transform one sequence into another one, and ToArray() to create an array from the result:

int[] numbers = { 1, 2, 3 };
string[] strings = numbers.Select(x => ToWords(x)).ToArray();

score:7

It's pretty straight forward. Just use the Select method:

var results = array.Select(ToWords).ToArray();

Note that unless you need an array you don't have to call ToArray. Most of the time you can use lazy evaluation on an IEnumerable<string> just as easily.

score:0

There are two different approaches - you can use Select extension method or you can use select clause:

var numbers = new List<int>();
var strings1 = from num in numbers select ToWords(num);
var strings2 = numbers.Select(ToWords);

both of them will return IEnumerable<>, which you can cast as you need (for example, with .ToArray() or .ToList()).

score:0

You could do something like this :

    public static string[] ProcessStrings(int[] intList)
    {
        return Array.ConvertAll<int, string>(intList, new Converter<int, string>(
        delegate(int number)
        {
            return ToWords();
        }));
    }

If it is a list then :

    public static List<string> ProcessStrings(List<int> intList)
    {
        return intList.ConvertAll<string>(new Converter<int, string>(
         delegate(int number)
         {
              return ToWords();
         }));
    }

score:0

Straight simple:

string[] wordsArray = array.ToList().ConvertAll(n => ToWords(n)).ToArray();

If you are OK with Lists, rather than arrays, you can skip ToList() and ToArray().

Lists are much more flexible than arrays, I see no reason on earth not to use them, except for specific cases.


Related Articles