How to return subtype in overridden method of subclass in C#?

Unfortunately no, covariant return types aren’t supported in C# for method overriding. (Ditto contravariant parameter types.)

If you’re implementing an interface you can implement it explicitly with the “weak” version and also provide a public version with the stronger contract. For simple overriding of a parent class, you don’t have this luxury I’m afraid 🙁

(EDIT: Marc has a reasonable solution – although it’s pretty ugly, and method hiding is generally a bad thing for readability. No offence meant, Marc 😉

I believe this is actually a CLR restriction, not just a language one – but I could well be wrong.

(As a matter of history, Java (the language) had the same restriction until 1.5 – but it gained covariance at the same time as generics.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)