foreach和list.ForEach的区别

平时写代码的时候要对一个List<T>进行遍历操作时,经常会纠结是要用foreach还是使用.ForEach方法。现在来搞清楚这二者之间的使用区别和性能上的差异。

一、使用

1.在foreach和list.ForEach中使用list.Remove()

在foreach中是不能使用list.Remove(),否则在进入下一个循环就会报异常,所以,如果有使用之后就必须break;

在.ForEach()中,要注意,在.net framework 4.5 会报异常:集合已修改;可能无法执行枚举操作。在.net framework4 3.5 3.0 2.0这几个版本下,可以直接使用list.Remove(),但如果在使用Remove()后下一项是不会遍历到,会自动用下下项。如下代码

 

 1         List<Person> list = new List<Person>(){
 2             new Person(){ Id=1,Name="1"},
 3             new Person(){ Id=2,Name="2"},
 4             new Person(){ Id=3,Name="3"},
 5             new Person(){ Id=4,Name="4"},
 6             };
 7             list.ForEach(item =>
 8             {
 9                 Console.WriteLine(string.Format("111--Id:{0},Name:{1}", item.Id, item.Name));
10                 if (item.Id == 2)
11                 {
12                     list.Remove(item);
13                     item.Id = item.Id * 10;
14                 }
15                 item.Id = item.Id * 10;
16                 Console.WriteLine(string.Format("222--Id:{0},Name:{1}", item.Id, item.Name));
17             });
18             Console.WriteLine("------------");
19             list.ForEach(item => Console.WriteLine(string.Format("Id:{0},Name:{1}", item.Id, item.Name)));    
View Code

结果:

111--Id:1,Name:1
222--Id:10,Name:1
111--Id:2,Name:2
222--Id:200,Name:2
111--Id:4,Name:4
222--Id:40,Name:4
------------
Id:10,Name:1
Id:3,Name:3
Id:40,Name:4

就是在id=2这项remove后还会对这项进行操作,但在进入下一项是id=4,而id=3这项没有遍历到

所以如果想删除id=2和id=3这两项,结果是id=3是删除不了,如下:

1     list.ForEach(item =>
2             {
3                 if (item.Id == 2 || item.Id==3)
4                 {
5                     list.Remove(item);
6                 }
7             });
8             list.ForEach(item => Console.WriteLine(string.Format("Id:{0},Name:{1}", item.Id, item.Name)));    
View Code

结果:

Id:1,Name:1
Id:3,Name:3
Id:4,Name:4

只删除了id=2这项,所以想删除list中的项,最好还是用for

1     for (int i = 0; i < list.Count; i++)
2             {
3                 if (list[i].Id == 2 || list[i].Id == 3)
4                 {
5                     list.Remove(list[i]);                    
6                     i--;
7                 }
8             }
View Code

注意删除后,i--;

或者使用list.RemoveAll(),如下:

1             list.RemoveAll(item => { return item.Id == 2 || item.Id == 3; });
2             list.ForEach(item => Console.WriteLine(string.Format("Id:{0},Name:{1}", item.Id, item.Name)));
View Code

所以,想删除list中的项,最好不用使用foreach和list.ForEach,而是使用for或list.RemoveAll

2.在list.ForEach()中不能使用continue或者break

如果在遍历到某个特殊项的时候,不用遍历后面的项,需要break,这种情况使用foreach

3.list.ForEach()的使用

foreach的使用就不说了。

看一下msdn上对list.ForEach的参数的说明:

System.Action<T>
要对 List<T> 的每个元素执行的 Action<T> 委托。

Action<T> 是对传递给它的对象执行某个操作的方法的委托。当前 List<T> 的元素被分别传递给 Action<T> 委托。msdn上的示例代码:

 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 }
View Code

所以,如果有需要用到委托方法,可以使用list.ForEach

平时使用list.ForEach一般都是用Lambda 表达式

二、性能

简单写了一个测试代码,循环一千万次,看下所用时间:

 1             List<Person> list = new List<Person>();
 2             for (int i = 0; i < 10000000; i++)
 3             {
 4                 list.Add(new Person() { Id = i, Name = i.ToString() });
 5             }
 6             Stopwatch watch = new Stopwatch();
 7             watch.Start();
 8             foreach (var item in list)
 9             {
10                 item.Id = item.Id + 1;
11             }
12             watch.Stop();
13             Console.WriteLine(string.Format("foreach执行时间:{0}", watch.Elapsed));            
14             watch = new Stopwatch();
15             watch.Start();
16             list.ForEach(item => { item.Id = item.Id + 1; });
17             watch.Stop();
18             Console.WriteLine(string.Format("list.ForEach执行时间:{0}", watch.Elapsed));
19             watch = new Stopwatch();
20             watch.Start();
21             for (int i = 0; i < list.Count; i++)
22             {
23                 list[i].Id = list[i].Id + 1;
24             }
25             watch.Stop();
26             Console.WriteLine(string.Format("for1执行时间:{0}", watch.Elapsed));
27             watch = new Stopwatch();
28             watch.Start();
29             int count = list.Count;
30             for (int i = 0; i < count; i++)
31             {
32                 list[i].Id = list[i].Id + 1;
33             }
34             watch.Stop();
35             Console.WriteLine(string.Format("for2执行时间:{0}", watch.Elapsed));
View Code

结果:

foreach执行时间:00:00:00.2760314
list.ForEach执行时间:00:00:00.2458242
for1执行时间:00:00:00.3641918
for2执行时间:00:00:00.1642685

效率最高是for2,for1和for2区别看代码就知道了,list.ForEach会比foreach略快一点,但差别并不大,所以在使用foreach和list.ForEach上可以不考虑性能上的差异。

总结,foreach和list.ForEach在性能上差异不大,在使用上实际也不会有很多差别,只是在几个特殊使用上会有所不同,但list.ForEach代码看上去会更新简洁一些。

转载于:https://www.cnblogs.com/myzper/p/3850903.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值