是否有列表<T>保证物品将按照添加的顺序退回? [英] Does a List&lt;T&gt; guarantee that items will be returned in the order they were added?

查看:28
本文介绍了是否有列表<T>保证物品将按照添加的顺序退回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List 是否总是保证项目将按照枚举时添加的顺序返回?

Does a List<T> always guarantee that items will be returned in the order they were added when enumerated?

更新:感谢大家的回答,让我放心.我使用 .NET Reflector 快速浏览了 List<T> 类(应该一开始就应该这样做),实际上底层存储是一个 T 数组代码> (T[]).

Updated: Thanks for all the answers folks, puts my mind at ease. I had a quick poke around the List<T> class with .NET Reflector (should've probably done that in the first place) and indeed the underlying store is an array of T (T[]).

推荐答案

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

The List is index based and new items will always be added to the end of the list. You can insert items at a certain index so the next items will move one position.

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

So yes, you can use it safely that way...

List(T) 类是泛型相当于 ArrayList 类.它实现 IList(T) 泛型使用大小为数组的接口根据需要动态增加.

The List(T) class is the generic equivalent of the ArrayList class. It implements the IList(T) generic interface using an array whose size is dynamically increased as required.

这个集合中的元素可以是使用整数索引访问.此集合中的索引是从零开始.

Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

List(T) 不保证是排序.您必须对 List(T) 进行排序在执行操作之前(例如BinarySearch) 需要 List(T)进行排序.

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

一个List(T)可以支持多个reader同时,只要集合未修改.通过集合进行枚举是本质上不是线程安全的程序.在极少数情况下,枚举与一个或多个竞争写访问,这是确保的唯一方法线程安全是锁定整个过程中收集枚举.允许集合被多个线程访问阅读和写作,你必须实现您自己的同步.

A List(T) can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

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

You can read more about it on MSDN.

这篇关于是否有列表<T>保证物品将按照添加的顺序退回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆