【C#】List.ForEach 方法

一边遍历list 可以用for 或者foreach去操作,后来发现list本身就有迭代的方法,ForEach

查看MSDN的介绍:ForEach 本身要传一个Action的委托

官方例子:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List names = new List();
        names.Add("Bruce");
        names.Add("Alfred");
        names.Add("Tim");
        names.Add("Richard");

        // Display the contents of the list using the Print method.
        names.ForEach(Print);

        // The following demonstrates the anonymous method feature of C#
        // to display the contents of the list to the console.
        names.ForEach(delegate(String name)
        {
            Console.WriteLine(name);
        });
    }

    private static void Print(string s)
    {
        Console.WriteLine(s);
    }
}


匿名函数使用ForEach:
xfcTarget :参数

public List XFCTargets = new List();
 XFCTargets.ForEach(xfcTarget =>
        {
            if (xfcTarget) xfcTarget.SetActive(active);
        });



你可能感兴趣的:(C#)