0

I normally see the use of,foreach @ generics as

    List<int> lst = new List<int>();
    lst.Add(10);
    lst.Add(20);
    lst.Add(30);
    lst.ForEach(x => Console.WriteLine(x));

How can i achieve something similar:

lst.ForEach(x => x *x ) ?

user196546
  • 2,513
  • 4
  • 23
  • 18

1 Answers1

4
lst.Select(x => x * x ).ToList();

Hope that helps,

Dan

Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
  • 1
    Just to add to this, Dan's code returns an IEnumerable rather than another List: if you specifically need another List, then check out List.ConvertAll. (Or call ToList() on Dan's expression of course.) – itowlson Nov 15 '09 at 06:49