简体   繁体   English

如何列出 <T> .AddRange实际添加所提供的范围?

[英]How does List<T>.AddRange actually add the range supplied?

I've looked around for a while now and can't seem to find any documentation that covers my concern. 我已经环顾了一段时间,似乎找不到涵盖我所关注问题的任何文档。

I am wondering if I should ever be concerned with the order a range supplied to List<T>.AddRange() is added to the collection. 我想知道我是否应该关心将提供给List<T>.AddRange()范围添加到集合中的顺序。 I can assume all day that it will be added in the sequential order I supplied; 我可以假设一整天都按照我提供的顺序添加它; that is, if I supply 0, 8, 5, 2, 9 it shouldn't ever be added in the order of 9, 5, 2, 0, 8 or anything other than 0, 8, 5, 2, 9 . 也就是说,如果我提供0, 8, 5, 2, 9它不应该在以往的顺序添加9, 5, 2, 0, 8或大于其它任何东西0, 8, 5, 2, 9

I've ran a test in a console application to determine the results using a post-increment in the AddRange method like so: 我已经在控制台应用程序中运行了一个测试,以便使用AddRange方法中的后增量来确定结果,如下AddRange

List<int> test = new List<int>();
for (int i = 0; i < 30;)
    test.AddRange(new int[] { i++, i++, i++ });

foreach (int i in test)
    Console.WriteLine(i);

This prints in sequential order as I expected it to from zero to 29 . 如我期望的那样,它按顺序打印,从zero29 However, my concern is that I'm unable to find documentation that shows me exactly how this range is added to the collection. 但是,我担心的是,我找不到能准确显示该范围如何添加到集合中的文档。 I would assume that it would be something along the lines of: 我认为这可能与以下内容类似:

foreach (int i in range)
    collection.Add(i);

Which would definitely preserve any order I've supplied, but I would like to ensure that this is actually what is occurring, or at least that what is occurring will never reorder the supplied range. 这肯定会保留我提供的任何订单,但我想确保这实际上是正在发生的事情,或者至少是正在发生的事情永远不会重新排序所提供的范围。


NOTE: 注意:

Apparently I haven't had enough coffee yet today and missed it while looking over the documentation for AddRange . 显然我今天还没有喝咖啡,在查看AddRange文档时错过了它。 Thank you all anyways, at least it's a great question for future readers that may be curious. 无论如何,还是谢谢你,至少对于可能好奇的未来读者来说,这是一个很好的问题。

The order of the elements in the collection is preserved in the List. 集合中元素的顺序保留在列表中。

It's the first line under the section Remarks at: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.addrange?view=netframework-4.7.2 它是“备注”部分下的第一行, 网址为: https : //docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.addrange?view=netframework-4.7.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM