2

Situation:

I implement an interface implicit and remove a property on the interface (later). There is no warning that this property should be removed on the implementation class.

I know I could implement the interface explicitly, but I would try to go around that.


EDIT: (added a question)

How can I be notified/warned/... that I maybe no longer need the member in the implementing class?

joerg
  • 717
  • 2
  • 8
  • 18
  • 1
    So what is the question here? – Ron Beyer Aug 11 '15 at 12:19
  • 2
    There's nothing built in to do that. You may be able to add a new, incompatible, property to the interface using the same name, then use the compiler errors to track down the offending code (then remove the new property) – Damien_The_Unbeliever Aug 11 '15 at 12:20
  • 2
    It is impossible to detect, as the class is still valid, regardless if the given property respects an interface or not. Explicit interface implementation will give you the compile-time error you seek, and if you need implicit exposure of the property/method, you can always have both implicit and explicit implementations. The latter is a manner to just have the indication the interface no longer supports a given method/property, so you can make "informed decision" while working with the implementing types – Ivaylo Slavov Aug 11 '15 at 12:22
  • Resharper can detect some dead code (solution wide analysis, but it is very slow) and add little icons showing that the property satisfies an interface. – Alexandre Borela Aug 11 '15 at 12:24
  • Visual Studio adds the interface-members in a #region... so it is obviously (at least for human) that it shouldn't be there anymore... is there a code-analysis-tool ore similar that could tell me that? – joerg Aug 11 '15 at 12:24
  • It's easy to identify when a class claims to implement an interface but has missing members. But note that you'll get no such warnings if you add an interface to a class that *happens* to already have compatible members - either just because it makes sense for that class to have them or because it also implements another interface that requires the same members. – Damien_The_Unbeliever Aug 11 '15 at 12:32

1 Answers1

1

There is no way the compiler can give you such a warning in C#. It would somehow need to have knowledge of past versions of the interface to know what method/property was removed and therefore identify possible candidates for removal.

And answering your commentary, you can not make the compiler / refactoring tool decide if any given method is a candidate simply based on the #region its defined in. There is absolutetly nothing that enforces any given method to be defined in any given region, its just visual sugar so refactoring based on regions would be completely unsafe.

Sometimes verbose languages do have advantages, and in this case VB with the implements keyword would make this a compile time error. In C#, you have to use explicitly implemented interfaces which is not a bad option at all. Read here for more details.

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • Maybe I am getting convinced to use explicit more often in future... but what about existing code? Even Resharper cannot make implicit --> explicit completely automated (as far as I could find out) – joerg Aug 11 '15 at 13:18
  • @joerg Well refactoring implicit to explicit is a major breaking change so I'm not sure that is an option you'd want to take with existing code. – InBetween Aug 11 '15 at 13:46
  • You are right... it's just very annoying to often find this no-longer-needed-properties in my implementations... ;) – joerg Aug 11 '15 at 13:56