List.ForEach()的使用

看例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List list = new List(new string[] { "111", "222" });

            list.ForEach(delegate (String item) { Console.WriteLine(item); });

            Work work = new Work();  

            list.ForEach(work.doWork);  

            list.ForEach(
                item => work.doWork
                (
                    () => { Console.WriteLine(item); } //this is Action object passed as parameter
                )
            );

            Console.Read();
        }

        public class Work
        {
            public void doWork(Action act)
            {
                act();
                Console.WriteLine("Pass in Action");
            }

            public void doWork(string item)
            {
                Console.WriteLine(item);
                Console.WriteLine("Pass in string");
            }
        }

    }
}

一共可以有3种方法:
      • 直接写函数代码
      • 调用其它类的函数,执行代码调用函数中
      • 调用其它类的函数,执行代码为调用者与调用函数代码混合
 

 

转载于:https://www.cnblogs.com/sungang3225/p/7613506.html

你可能感兴趣的:(List.ForEach()的使用)