您的位置:首页 > 编程语言 > C#

【C#】List<T>.ForEach 方法

2018-01-05 10:22 666 查看
一边遍历list 可以用for 或者foreach去操作,后来发现list本身就有迭代的方法,ForEach

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

官方例子:

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<String> names = new List<String>();
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<GameObject> XFCTargets = new List<GameObject>();
XFCTargets.ForEach(xfcTarget =>
{
if (xfcTarget) xfcTarget.SetActive(active);
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: