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

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

问题描述

列表< T> 始终保证以枚举时所添加的顺序返回商品?

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

更新 :感谢所有的答案,让我放心。我有一个快速戳的周围的列表< T> 类与.NET反射器(应该应该是这样做的第一位),确实底层存储是一个数组 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.

列表(T)不保证为
排序。在执行需要对列表(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)可以同时支持多个读者
,只要
集合未被修改。
枚举一个集合是
本质上不是线程安全的
过程。在一个罕见的情况下,
枚举与一个或多个
写访问争斗,确保
线程安全的唯一方法是在整个
期间锁定
集合枚举。要允许
的集合
被多个线程访问用于
的读写,您必须
实现自己的同步。

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

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

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