我试图创建一个基于自定义集合堆栈< T>
。当我看到堆栈< T>
[从元数据]在Visual Studio中,充分显示了堆栈< T>
工具的ICollection
,这需要它来实现的ICollection
的 CopyTo从(数组的数组,索引)
的方法,而是,它显示为具有的ICollection< T>
的 CopyTo从(T []数组,索引)
方法。有人可以解释为什么是这样的情况?
I'm trying to create a custom collection based on Stack<T>
. When I look at Stack<T>
[from metadata] in visual studio, it shows that Stack<T>
implements ICollection
, which would require it to implement ICollection
's CopyTo(Array array, index)
method, but instead, it is shown as having ICollection<T>
's CopyTo(T[] array, index)
method. Can someone explain why this is the case?
我试图创建一个集合,模仿堆栈&LT; T&GT;
pretty的巨资。当我实施的ICollection
作为栈做,它要求我使用 CopyTo从(数组的数组,指数)
的方法,但我的真正的希望是使用 CopyTo从(T []数组索引)
的方法,如堆栈&LT; T&GT;
一样。有没有一种方法来实现这一目标没有实现的ICollection&LT; T&GT;
I'm trying to create a collection that mimics Stack<T>
pretty heavily. When I implement ICollection
as stack does, it requires me to use the CopyTo(Array array, index)
method, but what I really want is to use the CopyTo(T[] array, index)
method, like Stack<T>
does. Is there a way to achieve this without implementing ICollection<T>
?
正如其他人写的,你可以使用显式接口实现,以满足您的非通用接口:
As others have written, you can use explicit interface implementation to satisfy your non-generic interface:
void ICollection.CopyTo(Array array, int arrayIndex)
{
var arrayOfT = array as T[];
if (arrayOfT == null)
throw new InvalidOperationException();
CopyTo(arrayOfT, arrayIndex); // calls your good generic method
}
上一篇:GCM重复的报文报文、GCM