难道一个列表< T>保证项目将在它们的添加顺序返回? [英] Does a List<T> guarantee that items will be returned in the order they were added?

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

问题描述

难道一个名单,其中,T> 始终保证项目将它们添加枚举时的顺序返回

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

更新:感谢所有的答案乡亲,把我放心。我周围的列表℃的快速戳; T&GT; 类.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...

列表(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)   执行操作(如前   二分查找)要求的列表(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.

一个列表(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

You can read more about it on MSDN.

这篇关于难道一个列表&LT; T&GT;保证项目将在它们的添加顺序返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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