6

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?

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>?

Thick_propheT
  • 1,003
  • 2
  • 10
  • 29
  • 9
    The methods are implemented explicitly. Explicitly implemented methods are not public and therefore would not show up in the metadata (which only lists public properties). – Jeff Mercado May 14 '12 at 19:29
  • 1
    You might try .NET Reflector to see the actual complete source code of `Stack`, including private methods. – mellamokb May 14 '12 at 19:31
  • @JeffMercado Wow... I can't believe it was that easy. I always wondered what "Explicitly Implement suchnsuch" meant. Thanks. – Thick_propheT May 14 '12 at 19:31
  • 1
    Posted the .NET 4.0 implementation of Stack if you want to take a look. http://pastebin.com/p2k4URtU – SPFiredrake May 14 '12 at 19:34
  • @SPFiredrake, how did you get that?! – MarcinJuraszek May 14 '12 at 19:46
  • 2
    @JeffMercado you should probably post that as an answer and briefly explain how to explicitly implement an interface so you can get the credit. – C.Evenhuis May 14 '12 at 19:47
  • @JeffMercado - your answer, which is good, is more useful as an answer than a comment. Strangely, I've seen more and more people use comments to answer questions lately. Go figure. – Peter Lillevold May 14 '12 at 20:42
  • @C.E: I'm swamped at work right now and would have otherwise provided a full answer. The best that I could do with the time I have is to throw in a comment. It's hard to force myself to put in a sub-standard answer (by my standards). – Jeff Mercado May 14 '12 at 20:46
  • 3
    @MarcinJuraszek - downloading Framework source from within VS has been around for a while. Take a look at this article: http://blogs.msdn.com/b/rscc/archive/2010/08/16/net-framework-4-reference-source-is-available.aspx – Peter Lillevold May 14 '12 at 20:50

2 Answers2

3

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
}
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
0

I guess the method CopyTo(Array array, index) is implemented explicitly. This means, you will only see that method if you see the object as an ICollection:

var collection = (ICollection)stack;
collection.CopyTo(array, 0);
Ivo
  • 8,172
  • 5
  • 27
  • 42