I have some List:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
I want to apply some transformation to elements of my list. I can do this in two ways:
List<int> list1 = list.Select(x => 2 * x).ToList();
List<int> list2 = list.ConvertAll(x => 2 * x).ToList();
What is the difference between these two ways?
Selectis a LINQ extension method and works on allIEnumerable<T>objects whereasConvertAllis implemented only byList<T>. TheConvertAllmethod exists since .NET 2.0 whereas LINQ was introduced with 3.5.You should favor
SelectoverConvertAllas it works for any kind of list, but they do the same basically.