【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:
public List<GameObject> XFCTargets = new List<GameObject>();
XFCTargets.ForEach(xfcTarget =>
{
if (xfcTarget) xfcTarget.SetActive(active);
});
查看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);
});
相关文章推荐
- <c:foreach>同时遍历两个List的方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- C#中泛型集合List<T>反序列化问题及解决方法
- C# List<T>自定义排序方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- C#List<Category>使用方法
- C# List<T>的嵌套和foreach的使用
- C# List<T> 自定义排序方法
- 【转载】[C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- C#泛型集合List<T>自带方法之排序
- C# List<T> Add方法循环添加时只保存了最后一次的数据(覆盖问题)
- List<T>的ForEach方法(可以使用“语句lambda”作为其参数),如下例子:
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- 用python模仿c#的List<>一组list筛选扩展方法
- Map<String,Map<String,List<String>>>结构取数据这个方法比Iterator和foreach效率稍高。
- C# List<string> 的Contains方法 是区分大小写的
- 深入理解 c# 第一章 使用扩展方法对 List<Product> 排序
- <c:foreach>同时遍历两个List的方法,List长度最好一致的
- <c:foreach>同时遍历两个List的方法