gpt4 book ai didi

.net - List 是否保证项目将按添加顺序返回?

转载 作者:行者123 更新时间:2023-12-03 05:24:24 25 4
gpt4 key购买 nike

List<T>吗?始终保证项目将按照枚举时添加的顺序返回?

已更新:感谢大家的回答,让我放心了。我快速浏览了一下List<T>带有 .NET Reflector 的类(可能应该首先这样做),实际上底层存储是一个 T 的数组。 (T[])。

最佳答案

列表是基于索引的,新项目将始终添加到列表的末尾。您可以在某个索引处插入项目,以便下一个项目将移动一个位置。

所以是的,您可以这样安全使用它......

The List(T) class is the genericequivalent of the ArrayList class. Itimplements the IList(T) genericinterface using an array whose size isdynamically increased as required.

Elements in this collection can beaccessed using an integer index.Indexes in this collection arezero-based.

The List(T) is not guaranteed to besorted. You must sort the List(T)before performing operations (such asBinarySearch) that require the List(T)to be sorted.

A List(T) can support multiple readersconcurrently, as long as thecollection is not modified.Enumerating through a collection isintrinsically not a thread-safeprocedure. In the rare case where anenumeration contends with one or morewrite accesses, the only way to ensurethread safety is to lock thecollection during the entireenumeration. To allow the collectionto be accessed by multiple threads forreading and writing, you mustimplement your own synchronization.

您可以在MSDN上阅读更多相关信息。 .

关于.net - List<T> 是否保证项目将按添加顺序返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/453006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com