Tony said:
Hello!
The generic class Queue<T> has the following header declaration
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
{
...
}
Where do you see the declaration? The actual source code for the
Queue<T> class has only IEnumerable<T> and ICollection. However, note
that the IL generated when the file is compiled includes the "flattened"
inheritance list, since the class itself is still required to implement
interfaces inherited by interfaces it directly declares as implemented.
So, even though the class is written with just IEnumerable<T> and
ICollection in the declaration, if you look at it with anything that
shows you the emitted IL, you'll see all the interfaces the class is
actually implementing, including IEnumerable in this case.
Note that the MSDN documentation is probably auto-generated from the IL
(some variation on Reflector I'd guess), and so there you will see all
the interfaces the class has implemented, whether because it's declared
them directly as an implemented interface, or because it's been
inherited via some declared implemented interface.
and IEnumerable has the following declaration
public interface IEnumerable<T> : IEnumerable
{
....
}
I can't understanbd why Queue<T> is implementing IEnumerable because
IEnumerable<T> is implementing IEnumerable.
No. IEnumerable<T> can't implement anything. It's an interface. It
does inherit the IEnumerable interface, but it's the Queue<T> class (for
example) that has to implement both IEnumerable<T> and IEnumerable.
When you're writing the code, you need not declare both IEnumerable<T>
and IEnumerable. The latter is implied by the former. And in fact, the
written code for Queue<T> does not declare IEnumerable as an implemented
interface. But it's implied by the IEnumerable<T> declaration and will
show up in the IL itself (and of course in the actual implementation of
the Queue said:
So for me adding IEnumerable to the right side of Queue<T> is unnessesary
because as I mentioned
IEnumerable<T> is already implementing IEnumerable.
So can somebody explain why Queue<T> is implementing IEnumerable when
IEnumerable<T> already is implementing IEnumerable
See above. IEnumerable<T> can't implement it. Only Queue<T> can. And
you see it in the IL and documentation because Queue<T> does in fact
implement both the IEnumerable<T> and IEnumerable interface.
It's true that it implements IEnumerable because it's required to in
order to also implement IEnumerable<T>, and it's also true that where
the class is actually _declared_, there is no need to declare both
IEnumerable<T> and IEnumerable. But where the class is actually
_declared_, it _doesn't_ declare IEnumerable.
You just seem to be looking at something other than the actual class
declaration.
Pete