inheritance - C#: Overriding return types -


Can return types be overridden in C #? If so, and if that is not why and what is a recommended way of doing this?

My case is that I have an abstract base class and its descendants I would like to do this (not exactly, actually, but as an example!):

  Public Interface Animal {Expression {get} }} Public Square AnimalBase {Public Virtual Poo Exchumium {get {Poo return (); }}} Public class Dog {// do not override, return normal PU like normal animals} public class Cat {public override radioactive poo Akkrment {Get New Redioaktivpu (back); }}}  

Radioactive Puzzle is available from the Poo .

The reason I know this is that those who cat to access the object, they Akskriment without the use of the property Poo < can / code> to Rediodkiy in pop while for example cat can still be part of a Animals directory, where users They may not have the necessary information or care about their radioactive poo that it is understood ...

Where I can see that the compiler does not allow it at least. So I think this is impossible but how do you solve it?

What about a common base class?

  Public Sector Poo {} Public Class RadioApu: Poo () Public Square BaseAnimal & lt; PooType & gt; Where PooType: Poo, new () {PooType excrement {get new PooType ()); }}} Public square dog: baseAngel & lt; Pu & gt; {} Public Class CAT: Base Engine & lt; Radio Active & gt; {}  

Edit : extension methods and a new solution using the marker interface ...

  public class Poo {} Public Class RadioAipoo: Poo {} // Just a marker interface, IPooProvider for receiving the POW-type public interface & lt; PooType & gt; PooType & gt; (This IPooProvider & LT; PooType & gt; iPooProvider) {} // extension method excrement public static class IPooProviderExtension {public static PooType StronglyTypedExcrement & lt where PooType to get the right type: E {BaseAnimal Animals = iPooProvider BaseAnimal; If (faucet == animal) {throw a new invalid agreement exception ("iPooProvider must be a base."); } Return (PooType) animal. Expand; }} Public Class BaseAngel {Public Virtual PU Industry {get {return new Poo ()); }}} Public Squared Dog: Basenimal, IPOprovider & lt; Pu & gt; {} Public Class CAT: Base Enamil, IPOprovider & lt; Radiote Pu & gt; {Public Override Pursuit {get new radioactivepoo ()); }}} Class program {static zero main (string [] args) {dog dog = new dog (); Poo dogPoo = dog.Excrement; Cat cat = new cat (); Radioactive cat cat = cat Strongly tampered with (); }}  

In such a way both dogs and cats are received from animals (as commented on in the comment, my first solution did not preserve inheritance).
It is necessary to mark the sections clearly marked by the marker interface, which is painful, but perhaps it can give you some ideas ...

Edit another @Svish: I explitly that modified the code to show expansion method in any way the fact that iPooProvider does not apply BaseAnimal legacy. What do you mean by "typed more strongly"?


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -