About the IList interface and method GetEnumerator()

T

Tony

Hello!

According to the documentation we have the following
interface IList : ICollection, IEnumerable
{
I can understand all the methods in this IList
}

interface ICollection : IEnumerable
{
Here in this interface ICollection we have a meny methods. One is called:
GetEnumerator()
}

interface IEnumerable
{
GetEnumerator()
}

Now to my question when a base class implements IList there is two methods
called GetEnumerator
from two different interface that must be implemented explicitly.
I just want to check if I have understood this correctly?

//Tony
 
M

Marc Gravell

No; ICollection does not provide GetEnumerator() - it only looks like
it does because it inherits IEnumerable. ICollection is defined as
(minus comments):

[ComVisible(true)]
public interface ICollection : IEnumerable
{
int Count { get; }
bool IsSynchronized { get; }
object SyncRoot { get; }
void CopyTo(Array array, int index);
}

There is only one GetEnumerator() method here; and even if there were
two, as long as the meaning is the same there isn't a problem. The
issue arrises when you look at generics, where IEnumerable<T> :
IEnumerable, and provides a second GetEnumerator() method that has the
same args - so *here* you usually need explicit implemetation
(generally of the less-specific IEnumerable.GetEnumerator(), so that
the more-specific IEnumerable<T>.GetEnumerator() is visible on the
main API).

Marc
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

According to the documentation we have the following
interface IList : ICollection, IEnumerable
{
    I can understand all the methods in this IList

}

interface ICollection : IEnumerable
{
Here in this interface ICollection we have a meny methods. One is called:
  GetEnumerator()

}

interface IEnumerable
{
     GetEnumerator()

}

Now to my question when a base class implements IList there is two methods
called GetEnumerator
from two different interface that must be implemented explicitly.
I just want to check if I have understood this correctly?

//Tony

Hi,

No, you will get only one.
I think that the best explatation is with an example, declare a class
that implement IList and select implement interface, you will see that
you will get three sections, one per UNIQUE interfaces you will
implement.

A possible problem though would be if two interfaces have a method
with the same signature but with different objectives, in this case
you need to use EXPLICIT interface declaration and you will get two
methods in your class
 

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