Why is AddRange faster than using a foreach loop?

后端 未结 10 2359
长情又很酷
长情又很酷 2020-11-29 02:19
var fillData = new List();
for (var i = 0; i < 100000; i++)
     fillData.Add(i);

var stopwatch1 = new Stopwatch();
stopwatch1.Start();

var autoFill          


        
10条回答
  •  既然无缘
    2020-11-29 02:38

    The AddRange extension does not iterate through each item, but applies each item as a whole. Both foreach and .AddRange have a purpose. Addrange will win the contest of speed for your current situation.

提交回复
热议问题