about understanding

T

Tony Johansson

Hello!

The generic class Queue<T> has the following header declaration
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
{
...
}

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.

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


//Tony
 
R

Rudy Velthuis

Tony said:
Hello!

The generic class Queue<T> has the following header declaration
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
{
..
}

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.

This is a Delphi thing. If you don't explicitly implement an interface,
it is not exposed by the class and QueryInterface and GetInterface
won't be able to resolve it.

--
Rudy Velthuis http://rvelthuis.de

"The supreme art of war is to subdue the enemy without
fighting."
-- Sun tzu
 
R

Rudy Velthuis

Tony said:
Hello!

The generic class Queue<T> has the following header declaration
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
{
..
}

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.

IEnumerable<T> does not implement anything. It is an interface. It is
derived from the non-generic IEnumerable, it doesn't implement it. The
only implementation there is is Queue<T>, a class.
--
Rudy Velthuis http://rvelthuis.de

"The process of preparing programs for a digital computer is
especially attractive, not only because it can be economically
and scientifically rewarding, but also because it can be an
aesthetic experience much like composing poetry or music."
-- Donald Knuth
 
T

Tony Johansson

You didn't understand what I wrote.

As can see on this we could actually be able to remove the non generic
intarface IEnumerable and we would get the same result because
IEnumerable<T> is already implementing IEnumerable
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
{
..
}

Do you understand this ?

//Tony
 
R

Rudy Velthuis

Tony said:
You didn't understand what I wrote.

As can see on this we could actually be able to remove the non
generic intarface IEnumerable and we would get the same result
because IEnumerable<T> is already implementing IEnumerable

And you did not understand what I wrote. IEnumerable<T> does not
implement anything.

Since both resulting enumerators have different versions of
GetCurrent(), it makes sense to implement both in Queue<T>.
--
Rudy Velthuis http://rvelthuis.de

"The pious pretense that evil does not exist only makes it
vague, enormous and menacing."
-- Aleister Crowley
 
P

Peter Duniho

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
 
A

Arne Vajhøj

Tony said:
Hello!

The generic class Queue<T> has the following header declaration
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
{
..
}

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.

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

1) Find out who wrote the class at Microsoft and ask him/her.

2) Let some people guess. My guess is that the author considered
it more readable to explicit list IEnumerable instead of the reader
having to lookup IEnumerable<T>.

Arne
 
P

Peter Duniho

Arne said:
[...]
So can somebody explain why Queue<T> is implementing IEnumerable when
IEnumerable<T> already is implementing IEnumerable

1) Find out who wrote the class at Microsoft and ask him/her. [...]

The person who wrote the class at Microsoft didn't declare Queue<T> as
implementing IEnumerable. Tony's making an assumption here that's not
valid.

Pete
 
K

kndg

Tony said:
Hello!

The generic class Queue<T> has the following header declaration
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
{
...
}

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.

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


//Tony

Hi Tony,

I think, sometimes, we should let go the finer details.
As you may know, you can have multiple interface inheritance and all the
interface (including the parent) get "flattened" by the compiler.
So below class ABC, BC and C is equivalent.

public interface IA {}
public interface IB : IA {}
public interface IC : IA, IB {}

public class ABC : IA, IB, IC {}

public class BC : IB, IC {}

public class C : IC {}

Regards.
 
T

Tony Johansson

Hello!

This was the answer that i wanted.
So it is really not nessesary to have this IEnumerable but is probably added
because of the above
suggestion.

PS: I was sloppy in my writing when I wrote that an interface implement
another interface.
It should as you pointed out inherited even though it's not a real
inheritance.

//Tony
 
P

Peter Duniho

Tony said:
Hello!

This was the answer that i wanted.

It may be the answer you wanted, but it's not the answer to the question
you asked. As I explained, the author of the code has not written what
you are claiming they have.

You should look for correct answers, not just those that are the ones
you wanted.
So it is really not nessesary to have this IEnumerable

That's correct, it's not necessary.
but is probably added
because of the above suggestion.

It wasn't added at all. The actual code for the Queue<T> doesn't
declare both interfaces.

Pete
 
K

kndg

Peter said:
[...]
It wasn't added at all. The actual code for the Queue<T> doesn't
declare both interfaces.

Pete

Hi Pete,

Do you have any idea how can I get the actual source code for Queue<T>
class? Looking at IL doesn't help as the compiler has flatten all the
interface list. I had just looked at Mono's implementation of Queue<T>
class but to my surprise their declaration is as below,

[ComVisible(false)]
[Serializable]
public class Queue<T> : IEnumerable <T>, ICollection, IEnumerable
{
....
}

Regards.
 
P

Peter Duniho

kndg said:
Do you have any idea how can I get the actual source code for Queue<T>
class? [...]

Two ways that I know of: enable source-level debugging for .NET and step
into a Queue<T> member (at which point you see the whole source file in
the debugger), or install the source code from Microsoft's source server
project and examine it locally from the source tree.

In this particular case, I found that VS couldn't find the source for
Queue<T> so I was left having to do the latter. Looks like the
source-level debugging is aging again (they had some problems in the
past with source code failing to download because of version issues).

See here for more details:
http://referencesource.microsoft.com/netframework.aspx
Looking at IL doesn't help as the compiler has flatten all the
interface list. I had just looked at Mono's implementation of Queue<T>
class but to my surprise their declaration is as below,

[ComVisible(false)]
[Serializable]
public class Queue<T> : IEnumerable <T>, ICollection, IEnumerable
{
....
}

Interesting. Looks like they may have blindly taken as their
specification some reference for Queue<T> (and probably other parts of
..NET) that are derived from the compiled MSIL (e.g. the MSDN
documentation), without considering what the most concise representation
of the class would be.

Not really a big deal. The redundant interface declaration doesn't
affect the actual class implementation. Just funny to see. :)

Pete
 
K

kndg

Peter said:
kndg said:
Do you have any idea how can I get the actual source code for Queue<T>
class? [...]

Two ways that I know of: enable source-level debugging for .NET and step
into a Queue<T> member (at which point you see the whole source file in
the debugger), or install the source code from Microsoft's source server
project and examine it locally from the source tree.

In this particular case, I found that VS couldn't find the source for
Queue<T> so I was left having to do the latter. Looks like the
source-level debugging is aging again (they had some problems in the
past with source code failing to download because of version issues).

See here for more details:
http://referencesource.microsoft.com/netframework.aspx

Great! I don't even know that was possible.
Anyway, the website seems down at the moment. I'll try to check again later.
Looking at IL doesn't help as the compiler has flatten all the
interface list. I had just looked at Mono's implementation of Queue<T>
class but to my surprise their declaration is as below,

[ComVisible(false)]
[Serializable]
public class Queue<T> : IEnumerable <T>, ICollection, IEnumerable
{
....
}

Interesting. Looks like they may have blindly taken as their
specification some reference for Queue<T> (and probably other parts of
..NET) that are derived from the compiled MSIL (e.g. the MSDN
documentation), without considering what the most concise representation
of the class would be.

Not really a big deal. The redundant interface declaration doesn't
affect the actual class implementation. Just funny to see. :)

Yeah, true.

Regards.
 

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