Monday, 30 April 2012

Beginning .Net , C# Tips : Convert List of Integer to List of String

There are many situations where you want to convert List<int> to List<string>.
Using Linq Method "ConvertAll" we can achieve in one sentence.
LINQ Namespace is System.Linq .

Here are sample Example For this:

C# Example :
    List<int> l1 = new List<int>(new int[] { 1, 2, 3 });
    List<string> l2 = l1.ConvertAll<string>(delegate(int i) { return i.ToString(); });

Here ConvertAll methods accepts a deligate and return converted list.

VB.net Example :
    Dim l1 As New List(Of Integer)(New Integer() {1, 2, 3})
    Dim l2 As List(Of String) = l1.ConvertAll(Of String)(Function(i As Integer) i.ToString())

No comments:

Post a Comment