Stack<T> 的 ToArray 方法是否保证按此顺序 return?
Is the ToArray method of a Stack<T> guaranteed to return in this order?
我有一个 Stack<T>
,我用它来堆叠整数元组。现在,我正在使用以下代码 return 堆栈副本,从最旧到最新排序:
public IEnumerable<Tuple<int,int>> GetAllTuples()
{
return this.ToArray<Tuple<int,int>>().Reverse<Tuple<int,int>>();
}
这行得通,但我担心 return 由 ToArray()
编辑的顺序可能无法在所有情况下得到保证。 ToArray()
的文档说明如下:
The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Pop.
是否可以安全地假设将来调用 ToArray()
(因此推而广之,.Reverse()
)会 return 相同的顺序?我对“类似于”有点反感。如果不是,我如何确保元组 return 从最旧到最新?
为了进一步说明我的观点,这里有一个例子:
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
int[] toArr = stack.ToArray<int>(); // Yields [5, 4, 3, 2, 1]
int[] toArrReverse = toArr.Reverse<int>(); // Yields [1, 2, 3, 4, 5], which is what I want.
根据文档,元素以后进先出 (LIFO) 的顺序复制到数组中,类似于对 Pop
的连续调用返回的元素的顺序。
因此,这看起来是一个完全定义订单的严格合同。没有评论说这是特定于实现的,将来可能会发生变化。
所以,我相信我们现在和将来都可以依赖当前的行为。
This Microsoft 文档中的一段说 ToArray
Copies the Stack to an array, in the same order Pop would return the
items.
确认您观察到的行为。
// Copies the Stack to an array, in the same order Pop would return the items.
public virtual Object[] ToArray()
{
Contract.Ensures(Contract.Result<Object[]>() != null);
Object[] objArray = new Object[_size];
int i = 0;
while(i < _size) {
objArray[i] = _array[_size-i-1];
i++;
}
return objArray;
}
编辑:当你考虑它时,它是有道理的——根据定义,从堆栈中取出项目的唯一方法是弹出它们(后进先出) .当所有项目都被取出时(或者只是像 ToArray 一样被复制),情况也是如此。这只会一次发生一个项目。如果它表现得不一样,恕我直言,它显然违反了 Stack 的核心思想。
我有一个 Stack<T>
,我用它来堆叠整数元组。现在,我正在使用以下代码 return 堆栈副本,从最旧到最新排序:
public IEnumerable<Tuple<int,int>> GetAllTuples()
{
return this.ToArray<Tuple<int,int>>().Reverse<Tuple<int,int>>();
}
这行得通,但我担心 return 由 ToArray()
编辑的顺序可能无法在所有情况下得到保证。 ToArray()
的文档说明如下:
The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Pop.
是否可以安全地假设将来调用 ToArray()
(因此推而广之,.Reverse()
)会 return 相同的顺序?我对“类似于”有点反感。如果不是,我如何确保元组 return 从最旧到最新?
为了进一步说明我的观点,这里有一个例子:
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
int[] toArr = stack.ToArray<int>(); // Yields [5, 4, 3, 2, 1]
int[] toArrReverse = toArr.Reverse<int>(); // Yields [1, 2, 3, 4, 5], which is what I want.
根据文档,元素以后进先出 (LIFO) 的顺序复制到数组中,类似于对 Pop
的连续调用返回的元素的顺序。
因此,这看起来是一个完全定义订单的严格合同。没有评论说这是特定于实现的,将来可能会发生变化。
所以,我相信我们现在和将来都可以依赖当前的行为。
This Microsoft 文档中的一段说 ToArray
Copies the Stack to an array, in the same order Pop would return the items.
确认您观察到的行为。
// Copies the Stack to an array, in the same order Pop would return the items.
public virtual Object[] ToArray()
{
Contract.Ensures(Contract.Result<Object[]>() != null);
Object[] objArray = new Object[_size];
int i = 0;
while(i < _size) {
objArray[i] = _array[_size-i-1];
i++;
}
return objArray;
}
编辑:当你考虑它时,它是有道理的——根据定义,从堆栈中取出项目的唯一方法是弹出它们(后进先出) .当所有项目都被取出时(或者只是像 ToArray 一样被复制),情况也是如此。这只会一次发生一个项目。如果它表现得不一样,恕我直言,它显然违反了 Stack 的核心思想。