- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 List<T>.InsertRange()
的代码在 .NET 4.0 框架中,当我注意到一个奇怪的特性时。以下是引用代码:
public void InsertRange(int index, IEnumerable<T> collection) {
if (collection==null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
}
if ((uint)index > (uint)_size) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
}
Contract.EndContractBlock();
ICollection<T> c = collection as ICollection<T>;
if( c != null ) { // if collection is ICollection<T>
int count = c.Count;
if (count > 0) {
EnsureCapacity(_size + count);
if (index < _size) {
Array.Copy(_items, index, _items, index + count, _size - index);
}
// If we're inserting a List into itself, we want to be able to deal with that.
if (this == c) {
// Copy first part of _items to insert location
Array.Copy(_items, 0, _items, index, index);
// Copy last part of _items back to inserted location
Array.Copy(_items, index+count, _items, index*2, _size-index);
}
else {
T[] itemsToInsert = new T[count];
c.CopyTo(itemsToInsert, 0);
itemsToInsert.CopyTo(_items, index);
}
_size += count;
}
}
else {
using(IEnumerator<T> en = collection.GetEnumerator()) {
while(en.MoveNext()) {
Insert(index++, en.Current);
}
}
}
_version++;
}
特别注意 _version 总是在函数结束时递增。这意味着在对 InsertRange 的任何非异常调用中,列表上正在进行的枚举将失效,即使列表未更改也是如此。例如,以下代码抛出:
static void Main(string [] args) {
var list = new List<object>() {1, 2 };
using(var enumerator = list.GetEnumerator()) {
if(enumerator.MoveNext())
Console.WriteLine(enumerator.Current);
list.InsertRange(1, new object[]{});
if(enumerator.MoveNext()) // ** InvalidOperationException
Console.WriteLine(enumerator.Current);
}
}
修改方法以使枚举不以这种方式失效根本不会增加执行时间,因为代码已经检查了 count
的大小。 .它可以重写如下:
public void InsertRange(int index, IEnumerable<T> collection) {
if (collection==null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
}
if ((uint)index > (uint)_size) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
}
Contract.EndContractBlock();
ICollection<T> c = collection as ICollection<T>;
if( c != null ) { // if collection is ICollection<T>
int count = c.Count;
if (count > 0) {
EnsureCapacity(_size + count);
if (index < _size) {
Array.Copy(_items, index, _items, index + count, _size - index);
}
// If we're inserting a List into itself, we want to be able to deal with that.
if (this == c) {
// Copy first part of _items to insert location
Array.Copy(_items, 0, _items, index, index);
// Copy last part of _items back to inserted location
Array.Copy(_items, index+count, _items, index*2, _size-index);
}
else {
T[] itemsToInsert = new T[count];
c.CopyTo(itemsToInsert, 0);
itemsToInsert.CopyTo(_items, index);
}
_size += count;
_version++;
}
}
else {
var inserted = false;
using(IEnumerator<T> en = collection.GetEnumerator()) {
while(en.MoveNext()) {
inserted = true;
Insert(index++, en.Current);
}
}
if (inserted) _version++;
}
}
它的唯一缺点是一个额外的局部变量(可能会被 JIT 到一个寄存器中),工作集可能增加 20 个字节,以及插入 IEnumerable
时不相关的额外 CPU 工作量。秒。如果需要避免额外的 bool 或循环内赋值,则插入 IEnumerable
s 可以执行为
if(en.MoveNext()) {
Insert(index++, en.Current);
_version++;
}
while(en.MoveNext()) {
Insert(index++, en.Current);
}
所以...
.NET 实现是预期行为,还是错误?
编辑:
我意识到,如果您在一个线程上进行枚举,而在另一个线程上修改线程,那么您就做错了。根据文档,这些情况下的行为是未定义的。然而,List<T>
帮程序员一个忙,并在这些情况下抛出异常。我不是在问 List<T>
正确遵循文档:确实如此。我想问的是它的实现方式是否不是 Microsoft 的意图。
如果InsertRange()
表现符合预期,那么 List<T>
有不一致的行为。 RemoveRange()
方法仅在实际删除项目时才使枚举无效:
static void Main(string [] args) {
var list = new List<object>() {1, 2 };
using(var enumerator = list.GetEnumerator()) {
if(enumerator.MoveNext())
Console.WriteLine(enumerator.Current);
list.RemoveRange(1, 0);
if(enumerator.MoveNext()) // ** Does not throw
Console.WriteLine(enumerator.Current);
}
}
最佳答案
我猜这是故意的。 C# 遵循“成功坑”设计 - 他们希望它很难犯错。
最终,现有设计可以更轻松地分析该方法的使用。那是什么意思?嗯:
你举的例子很简单,一眼就能看出它并没有真正修改列表。但在几乎所有真实世界的代码中,情况并非如此。插入的序列几乎肯定是动态创建的,并且几乎可以是一个空序列几乎是随机的。一个空序列真的应该有不同的行为吗?如果插入的序列为空,您的代码将起作用,但是当您在其中放入真实的东西时,咔哒一声。
想象一下,如果您首先编写这段代码并且您的所有序列都是空的;看起来它有效。然后,在任意时间之后,您有一个非空插入。现在你得到了异常(exception)。您插入问题和检测问题之间的距离可能非常大。
随着它抛出任何成功的调用,故障模式变得更容易检测。
关于c# - List<T>.InsertRange() 或设计决策中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11026735/
我想使用 R 预定义这样的列表 DATA<-list( list(list(),list(),list()), list(list(),list(),list()), list(list(),l
如何将一个列表添加到另一个列表,返回一个列表的列表? foo :: [a] -> [a] -> [[a]] 例如,我想要的结果是: foo [1,2] [3,4] 将是 [[1,2], [3,4]]。
我还没有在这里找到类似问题的解决方案,所以我会寻求你的帮助。 有 2 个列表,其中之一是列表列表: categories = ['APPLE', 'ORANGE', 'BANANA'] test_re
这个问题不同于Converting list of lists / nested lists to list of lists without nesting (这会产生一组非常具体的响应,但无法解决
原始列表转换为 List正好。为什么原始列表的列表不能转换为 List 的列表? { // works List raw = null; List wild = raw; } {
在下面的代码中,get()被调用并将其结果分配给类型为 List> 的变量. get()返回 List>并在类型参数为 T 的实例上调用设置为 ? ,所以它应该适合。 import java.util
原始列表转换为 List正好。为什么原始列表的列表不能转换为 List 的列表? { // works List raw = null; List wild = raw; } {
在insufficiently-polymorphic 作者说: def foo[A](fst: List[A], snd: List[A]): List[A] There are fewer way
我有下面的代码有效。 class ListManipulate(val list: List, val blockCount: Int) { val result: MutableList>
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
在 scala (2.9) 中转换列表列表的最佳方法是什么? 我有一个 list : List[List[A]] 我想转换成 List[A] 如何递归地实现这一点?或者还有其他更好的办法吗? 最佳答案
我编写了这个函数来确定给定元素是否存储在元组列表的列表中,但目前它只搜索第一个列表。我将如何搜索其余列表? fun findItem (name : command, ((x,y)::firstlis
我创建了一个类名 objectA,它有 4 个变量:约会时间;字符串文本;变量 1,变量 2 我需要创建一个 ObjectA() 列表。然后首先按时间对它们进行分组,其次按 var1,然后按 var2
我有一套说法 char={'J','A'} 和列表的列表 content = [[1,'J', 2], [2, 'K', 3], [2, 'A', 3], [3,'A', 9], [5, 'J', 9
我有以下列表 List >>> titles = new ArrayList >>> ();我想访问它的元素,但我不知道该怎么做.. 该列表有 1 个元素,它又包含 3 个元素,这 3 个元素中的
转换 List[List[Long]] 的最佳方法是什么?到 List[List[Int]]在斯卡拉? 例如,给定以下类型列表 List[List[Long]] val l: List[List[Lo
我有一个来自 Filereader (String) 的 List-List,如何将其转换为 List-List (Double):我必须返回一个包含 line-Array 的第一个 Values 的
我收集了List> 。我需要将其转换为List> 。这是我尝试过的, List> dataOne = GetDataOne(); var dataTwo = dataOne.Select(x => x
这个问题在这里已经有了答案: Cannot convert from List to List> (3 个答案) 关闭 7 年前。 我没有得到这段代码以任何方式编译: List a = new Ar
这个问题在这里已经有了答案: Cannot convert from List to List> (3 个答案) 关闭 7 年前。 我没有得到这段代码以任何方式编译: List a = new Ar
我是一名优秀的程序员,十分优秀!