How does LinkedList Hide Add from ICollection<T> interface?

L

Lee

In trying to dupicate System.Collections.Generic.LinkedList, I hit a
snag.

LinkedList implements the ICollection<T> interface yet there is no Add
() method that is required by the interface.
Instead LinkedList has four different functions to add elements to the
list.

You can prove that the method Add exists by casting the the LinkedList
to ICollection.

How can I, if possible, replicate this is C#.

L. Lee Saunders
http://oldschooldotnet.blogspot.com
 
A

Anthony Jones

Lee said:
In trying to dupicate System.Collections.Generic.LinkedList, I hit a
snag.

LinkedList implements the ICollection<T> interface yet there is no Add
() method that is required by the interface.
Instead LinkedList has four different functions to add elements to the
list.

You can prove that the method Add exists by casting the the LinkedList
to ICollection.

How can I, if possible, replicate this is C#.


Use an explicit implementation in your class:-

void ICollection.Add<T>(T value)
{

}

This satisifies the contract for implementing ICollection<T> but Add won't
be available on your class.
 
G

Göran Andersson

Lee said:
In trying to dupicate System.Collections.Generic.LinkedList, I hit a
snag.

LinkedList implements the ICollection<T> interface yet there is no Add
() method that is required by the interface.
Instead LinkedList has four different functions to add elements to the
list.

You can prove that the method Add exists by casting the the LinkedList
to ICollection.

How can I, if possible, replicate this is C#.

L. Lee Saunders
http://oldschooldotnet.blogspot.com

There is an Add method, but it's only available if you access the object
through an ICollection<T> reference.

You accomplish this by implementing the method explicitly for the interface:

public void ICollection<T>.Add(T item) {
...
}
 
A

Anthony Jones

Göran Andersson said:
There is an Add method, but it's only available if you access the object
through an ICollection<T> reference.

You accomplish this by implementing the method explicitly for the
interface:

public void ICollection<T>.Add(T item) {
...
}

Although you don't need the public keyword, in fact it generates a compilier
error. An explicit interface member has to by definition be public.
 
L

Lee

Thank you Gentlemen, void ICollection<T>.Add(...) Works fantatically.

So from how to why (or what). What am I doing when I declare an
interface member explicitly? Is there an MSDN page that describes
this?

So, I guess they questions are now: Why does this work? How does
this work? (I've never seen any tutorial that explains this. Any
links would be appreciated)

L. Lee Saunders
http://oldschooldotnet.blogspot.com
 
J

Jon Skeet [C# MVP]

Although you don't need the public keyword, in fact it generates a compilier
error.  An explicit interface member has to by definition be public.

Well, it's *sort of* public - and sort of private. The spec even
acknowledges this oddity, in section 13.4.1:

<quote>
Explicit interface member implementations have different accessibility
characteristics than other members. Because explicit interface member
implementations are never accessible through their fully qualified
name in a method invocation or a property access, they are in a sense
private. However, since they can be accessed through an interface
instance, they are in a sense also public.
</quote>

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top