Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding an abstract property with a derived return type in c#

I have four classes. Request, DerivedRequest, Handler, DerivedHandler. The Handler class has a property with the following declaration:

public abstract Request request { get; set; } 

The DerivedHandler needs to override this property so that it returns DerivedRequest instead:

public override DerivedRequest request { get; set; } 

Does anyone have any ideas about how to make this work?

like image 416
Trevor Avatar asked Jun 14 '11 22:06

Trevor


People also ask

Can abstract method be overridden in derived class?

You can't. That would violate the accessibility level declared in class A.

Can overridden method have different return type C#?

You cannot change the return type of method during overriding.

What is abstract overriding?

Abstract methods don't have any implementation and require non-abstract derived classes to implement them. Of course they are allowed only in abstract classes. Override allows a method to override a virtual or abstract method from its base class.

Can you override properties in C#?

An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property. Beginning with C# 9.0, read-only overriding properties support covariant return types. The overridden property must be virtual , abstract , or override .


2 Answers

This isn't really a good way to structure things. Do one of the following

1) Just don't change the return type, and override it normally in the subclass. In DerivedHandler you can return an instance of DerivedRequest using the base class signature of Request. Any client code using this can choose to cast it to DerivedRequest if they want to.

2) Use generics instead if they are not supposed to be polymorphic.

public abstract class HandlerBase<T> where T: Request {     public abstract T Request {get;set;} }  public class Handler: HandlerBase<Request>()  public class DerivedHandler: HandlerBase<DerivedRequest>() 
like image 102
Jamie Treworgy Avatar answered Sep 18 '22 23:09

Jamie Treworgy


In the C# language you are not allowed to change the signature of an inherited method, unless you substitute it with another method with the same name. This technique is referred to as "member hiding" or "shadowing".

If you are using .NET 2.0 or later, you could solve this problem by turning the return type of the Request property into a generic type parameter of the Handler class. The DerivedHandler class would then specify the DerivedRequest class as argument for that type parameter.

Here's an example:

// Handler.cs public class Handler<TRequest> where TRequest : Request {     public TRequest Request { get; set; } }  // DerivedHandler.cs public class DerivedHandler : Handler<DerivedRequest> { } 
like image 21
Enrico Campidoglio Avatar answered Sep 19 '22 23:09

Enrico Campidoglio