(.NET) List<T>.ForEach

C#/Winform 2017. 4. 15. 22:01
728x90
728x170

  // List 를 선언하고

            List<person> pList = new List<person>()
            {
                new person(){Name="a", Age=1},
                new person(){Name="b", Age=2},
                new person(){Name="c", Age=3},
                new person(){Name="d", Age=4}
            };

 

  // Foreach 에서 수행할 Action 을 정의

            Action<person> print = person => { Console.WriteLine("{0},{1}",person.Name, person.Age); };

 

            // 수행

            pList.ForEach(print);

 

  // Action 을 정의하지 않고 아래와 같이 쓸수도 있다. 위 두줄이 한줄로

  pList.ForEach(person => Console.WriteLine("{0},{1}",person.Name, person.Age));

 

결과

a,1
b,2
c,3
d,4

 

728x90
그리드형
Posted by kjun
,