ConvertAll using lamba

by 4. July 2009 18:41

When we are using generic lists, we always used a delegate when converting a List<int> to a List<string>.  This is very easy using the know ConvertAll method.
Normally, we should call it like this:

List<int> l1 = new List<int>(new int[] { 1,2,3 } );
List<string> l2 = l1.ConvertAll<int>(delegate(string s) { return Convert.ToInt32(s); });

Now that we are using .Net3.5 we can use also lambas, so your code changed now to this:

l2= l1.ConvertAll(s => return Convert.ToInt32(s) );

Pretty readable imho…

Tags: , ,

C#

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen