list.Take(100).ToList() vs. list.GetRange(0,100)

前端 未结 5 1744
臣服心动
臣服心动 2021-02-19 11:57
List attendees = new List();
foreach ...
// Error: \"There are too many target users in the email address array\"
// for more tha         


        
5条回答
  •  后悔当初
    2021-02-19 12:36

    The only difference is that List.GetRange is more efficient than Take(n).ToList() since it already knows the size of the new list whereas the LINQ methods don't know it's size.

    So ToList enumerates the sequence and adds the items to a new list with a doubling algorithm increasing the backing array consecutively. List.GetRange can create the proper list with the right initial size beforehand and then uses Array.Copy to copy the subset of the source list into the new list [source].

提交回复
热议问题