是C#List< T>上元素的顺序吗?确定性的? [英] Is the order of elements on a C# List<T> deterministic?

查看:66
本文介绍了是C#List< T>上元素的顺序吗?确定性的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想别的,但是最近我需要知道:

I've always thought otherwise, but recently I had the need to know:

如果我按特定顺序将元素添加到列表中,是否可以保证始终以相同的顺序查找元素?

If I add elements to a list in a certain order, am I guaranteed to find then always on the same order?

谢谢!

推荐答案

是;您可以控制 List< T> 的顺序.

Yes; you control the ordering of a List<T>.

您可以假定带有 list [int] 索引器的任何.NET集合都具有可预测的顺序;否则,数字索引将毫无意义.相比之下,不可能将数字索引与 Dictionary< K,V> 一起使用,并且在枚举字典时,不能保证其顺序.

You can assume that any .NET collection with a list[int] indexer has a predictable ordering; otherwise, the numerical index wouldn't make any sense. By comparison, it's not possible to use a numerical index with a Dictionary<K,V>, and when you enumerate a dictionary, the ordering isn't guaranteed.

这篇关于是C#List&lt; T&gt;上元素的顺序吗?确定性的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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