Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a warning when only one of a pair of methods or properties is overridden

I have a c#-class which provides virtual operations. For each operation exists a synchronous and an asynchronous version.

public class Foo{
   public virtual void Bar(){..};
   public virtual Task BarAsync(){..};

   ...
}

I would like to have the compiler showing a warning if only one version of the operation is overridden (the synchronous or the asynchronous version of the operation) such as the compiler warns when one overrides Equals without overriding GetHashCode or vice versa.

Questioned more broadly: Is it possible to enforce that overriding one method or property enforces the overriding of other properties or methods (via compiler warnings).

like image 835
HCL Avatar asked May 30 '16 10:05

HCL


People also ask

What happens when a method overrides another method C#?

override (C# reference) An override method provides a new implementation of the method inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. An override method must have the same signature as the overridden base method.

Can you override a getter?

You can always manually disable getter/setter generation for any field by using the special AccessLevel. NONE access level. This lets you override the behaviour of a @Getter , @Setter or @Data annotation on a class.

What is the difference between the new and override keywords when overriding a class method?

In C#, a method in a derived class can have the same name as a method in the base class. You can specify how the methods interact by using the new and override keywords. The override modifier extends the base class virtual method, and the new modifier hides an accessible base class method.


1 Answers

Although this is not an answer to your actual question I´m asking for an approach where you do not even need the warning.

Why not create one abstract classes with overridable members and one sealed without:

public class Foo{
   public virtual void Bar(){..}
   public virtual Task BarAsync(){..}
}

public abstract class ImplementIt : Foo {
   public abstract override void Bar();
   public abstract override Task BarAsync();
}
public sealed class DoNotImplementIt : Foo {
   public override void Bar() {..}
   public override Task BarAsync() {..}
}

Now client can design if he needs an implementation of Foo whith your default behaviour (= DoNotImplementIt) or if he needs a customizable version using ImplementIt. In the former case he is forced to override the members in the latter case not.

This approach is far cleaner to your API-user as he knows what to override from the inheritance-chain instead of relying on messy warnings which no-one actually even takes care of.

An even better approach would be to define Foo as an interface which both ImplementIt and DoNotImplementIt implement (sounds weird, however you get it). Saves you from this abstract override. This way you can also hide your interface from the outside by making it internal and only make make the implementing classes accessable from the API.

like image 90
MakePeaceGreatAgain Avatar answered Sep 28 '22 19:09

MakePeaceGreatAgain