我從方法返回IList<T>
。列表<T>。插入與堆棧<T> .Push
在調用類中,我想在列表/堆棧的開始處插入一個值。
在List<T>
中插入值比在Stack<T>
中推送值慢。但要使用堆棧,我需要解開它。
所以問題是哪一個更好,unbox或使用Insert
在List<T>
?哪一個更昂貴?
class MyClass
{
}
IList<MyClass> Method1()
{
}
class MainClass
{
List<MyClass> list = (List<MyClass>)Method1();
list.insert(0,new MyClass{...}); //insert at the start.
Stack<MyClass> stack = (Stack<MyClass>)Method1();
stack.Push(new MyClass{...}); //insert at the start
}
['Stack'](http://msdn.microsoft.com/en-us/library/3278tedw.aspx)沒有實現'IList'接口,所以應該總是失敗。 –
堆棧沒有實現IList ,所以這整個問題是沒有意義的。 IList 說:「我的行爲像一個列表,所以如果你願意,可以通過索引隨時訪問我。」堆棧說:「我是一個堆棧,只能從頂部訪問我*。」有點問題,不是嗎? –
dlev
我很抱歉,我沒有運行代碼,但它編譯並編寫了SO.My Bad的問題。 –