Thursday, August 02, 2007

Array.ConvertAll and List.ConvertAll

Just noticed this handy method:
using System;
using System.Collections.Generic;

class Test
{
static void Main()
{
int[] numbers = new int[] { 65, 66, 67 };
char[] chars = Array.ConvertAll(numbers, delegate(int number)
{ return (char)number; });

//Print "ABC"
Array.ForEach(chars, delegate(char ch) { Console.Write(ch); });

Console.WriteLine();
List<int> numberList = new List<int>(numbers);
List<char> charList = numberList.ConvertAll(delegate(int number)
{ return (char)number; });

//Print "ABC"
charList.ForEach(delegate(char ch) { Console.Write(ch); });

Console.Read();
}
}