排名
7
文章
192
粉丝
15
评论
16
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

- //ForEach的原理就是使用循环
-
- //委托使用循环
- public static void MyForEach<T>(this List<T> item, Action<T> action)
- {
- for (int i = 0; i < item.Count; i++)
- {
- action(item[i]);
- }
- }
- //List也可以替换为List的父类IEnumerable(可使用的类型更广)
- public static void MyForEach<T>(this IEnumerable<T> list, Action<T> action)
- {
- foreach (T item in list)
- {
- action(item);
- }
- }
-
-
-
-
-
-
- //使用自定义的ForEach
- list.MyForEach(item =>
- {
- Console.WriteLine(item.UserName, item.Age, item.Address));
- });
评价