Csharp/CSharp Tutorial/Data Structure/Array ConvertAll — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:15, 26 мая 2010

Array.ConvertAll: convert all elements from one type to another type

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();
    }
}
Converted to doubles: 75.3 25.999 105.25

Array.ConvertAll: convert all string elements from lower case to upper case

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();
    }
}
Before converting to-upper: Blah Foo Canonical
Converted to upper-case: BLAH FOO CANONICAL