Convert elements in a List with custom function in CSharp

Description

The following code shows how to convert elements in a List with custom function.

Example


using System;/*ww  w. ja v  a2 s. c o  m*/
using System.Collections.Generic;

class ListConvertAll
{
    static double TakeSquareRoot(int x)
    {
        return Math.Sqrt(x);
    }

    static void Main()
    {
        List<int> integers = new List<int>();
        integers.Add(1);
        integers.Add(2);
        integers.Add(3);
        integers.Add(4);

        Converter<int, double> converter = TakeSquareRoot;
        List<double> doubles = integers.ConvertAll<double>(converter);

        foreach (double d in doubles)
        {
            Console.WriteLine(d);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary