ForEach

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

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

官方例子:

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. class Program  
  5. {  
  6.     static void Main()  
  7.     {  
  8.         List<String> names = new List<String>();  
  9.         names.Add("Bruce");  
  10.         names.Add("Alfred");  
  11.         names.Add("Tim");  
  12.         names.Add("Richard");  
  13.   
  14.         // Display the contents of the list using the Print method.  
  15.         names.ForEach(Print);  
  16.   
  17.         // The following demonstrates the anonymous method feature of C#  
  18.         // to display the contents of the list to the console.  
  19.         names.ForEach(delegate(String name)  
  20.         {  
  21.             Console.WriteLine(name);  
  22.         });  
  23.     }  
  24.   
  25.     private static void Print(string s)  
  26.     {  
  27.         Console.WriteLine(s);  
  28.     }  
  29. }  


匿名函数使用ForEach:
[csharp] view plain copy
  1. xfcTarget :参数  

[csharp] view plain copy
  1. public List<GameObject> XFCTargets = new List<GameObject>();  
  2.  XFCTargets.ForEach(xfcTarget =>  
  3.         {  
  4.             if (xfcTarget) xfcTarget.SetActive(active);  
  5.         });  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值