CS0506: Cannot override GetEnumerator() in subclass of ReadOnlyCollectionBase

  • Thread starter Thread starter Dennis Homann
  • Start date Start date
D

Dennis Homann

Hi all,

I do understand the need for virtual/override/new and also when to use what.
.... at least I thought so.
Why does the following code result in a CS0506 compiler error (should
translate to sth like "ReadOnlyCollectionBase.GetEnumerator() is not
declared as virtual, abstract, or override")?

public class MyCollection : ReadOnlyCollectionBase {
public override IEnumerator GetEnumerator() {
return null;
}
}

Please ignore the fact that this class is useless. I just tried to create a
minimal example.

According to the documentation and ildasm
System.Collections.ReadOnlyCollection.GetEnumerator() IS declared as
virtual.

Thanks,
Dennis
 
Dennis Homann said:
I do understand the need for virtual/override/new and also when to use what.
... at least I thought so.
Why does the following code result in a CS0506 compiler error (should
translate to sth like "ReadOnlyCollectionBase.GetEnumerator() is not
declared as virtual, abstract, or override")?

Because unfortunately the documentation is wrong - it's *not* virtual.

Don't be fooled my ildasm showing the "virtual" modifier - that shows
that it's *called* virtually, not that it *is* virtual, IIRC. All
rather confusing. The important modifier here is "final", which shows
it's not virtual.
 
Thanks, Jon. It all makes sense now.

- Dennis

Jon Skeet said:
Because unfortunately the documentation is wrong - it's *not* virtual.

Don't be fooled my ildasm showing the "virtual" modifier - that shows
that it's *called* virtually, not that it *is* virtual, IIRC. All
rather confusing. The important modifier here is "final", which shows
it's not virtual.
 
Back
Top