架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 22578|回复: 0

[资料] c# ForEach的用法

[复制链接]
发表于 2016-8-3 13:45:08 | 显示全部楼层 |阅读模式
  1. public class HomeController : Controller
  2.     {
  3.         // GET: Home
  4.         public ActionResult Index()
  5.         {
  6.             //return new NotFoundResult();

  7.             return View();
  8.         }

  9.         public ActionResult Error()
  10.         {
  11.             return View();
  12.         }

  13.         public ActionResult Test()
  14.         {
  15.             List<T1> list = new List<T1>() {
  16.                 new T1() {  id=1,name="张山"},
  17.                  new T1() {  id=2,name="张山"},
  18.                   new T1() {  id=1,name="张山"},
  19.                    new T1() {  id=2,name="张山"},
  20.                     new T1() {  id=2,name="张山"},
  21.             };
  22.             return Json(list, JsonRequestBehavior.AllowGet);
  23.         }

  24.         public ActionResult Test1()
  25.         {
  26.             List<T1> list = new List<T1>() {
  27.                 new T1() {  id=1,name="张山"},
  28.                  new T1() {  id=2,name="张山"},
  29.                   new T1() {  id=1,name="张山"},
  30.                    new T1() {  id=2,name="张山"},
  31.                     new T1() {  id=2,name="张山"},
  32.             };
  33.             List<T1> list2 = new List<T1>();
  34.             list.ForEach(m => list2.Add(m));
  35.             return Json(list2, JsonRequestBehavior.AllowGet);
  36.         }

  37.         private void Th(T1 t)
  38.         {

  39.         }
  40.     }

  41.     public class T1
  42.     {
  43.         public int id { get; set; }
  44.         public string name { get; set; }
  45.     }
复制代码


自己随便写的,我感觉,一般循环做什么事情的时候用这个玩意比较好。

下面是微软给的代码:

  1. 所需的主题如下所示。但此主题未包含在此库中。
  2. List<T>.ForEach 方法

  3. 其他版本
  4. 2013/12/13
  5. 对 List<T> 的每个元素执行指定操作。
  6. Namespace:  System.Collections.Generic
  7. 程序集:  mscorlib(位于 mscorlib.dll 中)
  8. 语法
  9. C#VB
  10. public void ForEach(
  11.         Action<T> action
  12. )
  13. 参数
  14. action
  15. 类型: System.Action<T>
  16. 要对 List<T> 的每个元素执行的 Action<T> 委托。
  17. 例外
  18. 异常        条件
  19. ArgumentNullException       
  20. action 为 null。
  21. 备注
  22. Action<T> 是对方法的委托,它对传递给自己的对象执行操作。当前 List<T> 的元素被分别传递给 Action<T> 委托。
  23. 此方法的运算复杂度为 O(n),其中 n 是 Count。
  24. 示例
  25. 下面的示例演示如何使用 Action<T> 委托来打印 List<T> 对象的内容。在此示例中,Print 方法用于将列表的内容显示到控制台。
  26. 说明注意:
  27. 除了使用 Print 方法显示内容外,该 C# 示例还演示了如何使用匿名方法将结果显示到控制台。
  28. C#VB

  29. using System;
  30. using System.Collections.Generic;

  31. class Example
  32. {
  33.    private static System.Windows.Controls.TextBlock outputBlock;

  34.    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
  35.    {

  36.       Example.outputBlock = outputBlock;
  37.       List<String> names = new List<String>();
  38.       names.Add("Bruce");
  39.       names.Add("Alfred");
  40.       names.Add("Tim");
  41.       names.Add("Richard");

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

  44.       // The following demonstrates the anonymous method feature of C#
  45.       // to display the contents of the list.
  46.       names.ForEach(delegate(String name)
  47.       {
  48.          outputBlock.Text += name + "\n";
  49.       });
  50.    }

  51.    private static void Print(string s)
  52.    {
  53.       outputBlock.Text += s + "\n";
  54.    }
  55. }
  56. /* This code will produce output similar to the following:
  57. * Bruce
  58. * Alfred
  59. * Tim
  60. * Richard
  61. * Bruce
  62. * Alfred
  63. * Tim
  64. * Richard
  65. */
复制代码






上一篇:nhibernate中的many-to-many 和one-to-many还有many-to-one分别是什么意思
下一篇:winform下载远程图片并显示
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-9-1 21:45

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表