Csharp/CSharp Tutorial/Data Structure/Array ConvertAll
Array.ConvertAll: convert all elements from one type to another type
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text;
public class MainClass {
public static void Main() { string[] strArray = new string[] { "75.3", "25.999", "105.25" }; double[] doubleArray = Array.ConvertAll<string, double>(strArray, Convert.ToDouble); Console.Write("Converted to doubles: "); Array.ForEach<double>(doubleArray, delegate(double x) { Console.Write(x + " "); }); Console.WriteLine(); }
}</source>
Converted to doubles: 75.3 25.999 105.25
Array.ConvertAll: convert all string elements from lower case to upper case
<source lang="csharp">using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text;
public class MainClass {
public static void Main() { string[] strArray2 = new string[] { "Blah", "Foo", "Canonical" }; Console.Write("Before converting to-upper: "); Array.ForEach<string>(strArray2, delegate(string x) { Console.Write(x + " "); }); Console.WriteLine(); string[] revArray = Array.ConvertAll<string, string>(strArray2, delegate(string s) { return s.ToUpper(); }); Console.Write("Converted to upper-case: "); Array.ForEach<string>(revArray, delegate(string x) { Console.Write(x + " "); }); Console.WriteLine(); }
}</source>
Before converting to-upper: Blah Foo Canonical Converted to upper-case: BLAH FOO CANONICAL