question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Public vs explicit implementation in handler methods

See original GitHub issue

IMO, handler methods should be private or protected but never public. These are not supposed to be called explicitly by the user.

Handlers have to implement interfaces (for example IInputClickHandler) which forces the methods to be public:

public class MyHandler: MonoBehaviour, IInputClickHandler
{
  public void OnInputClicked(InputEventData eventData)
  {
    ...
  }
}

Or implemented explicitly:

public class MyHandler: MonoBehaviour, IInputClickHandler
{
  void IInputClickHandler.OnInputClicked(InputEventData eventData)
  {
    ...
  }
}

When implemented explicitly, the method is still public but a bit hidden. They are only available for references of that interface type but, in that case, you explicitly want to call those methods anyway.

Unity does an awkward thing with its methods (Awake, Start, OnDestroy and so on) that calls them although they can be private and without them being defined in MonoBehaviour or any other interface (against all strongly-typed compiler rules) but to achieve more performance. Unless it does the same optimizations for the public interface methods, I would favor the use of explicit interface implementation.

The use of explicit implementation also makes it easier, when reading the code, to understand what interface is being implemented.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
luis-valverde-mscommented, Apr 3, 2019

+1 to the idea of implementing interfaces explicitly. Besides the advantages already mentioned, implementing an interface explicitly means that removing something (e.g.: method or property) from the interface automatically triggers a build error in implementations instead of silently leaving them with stale code.

0reactions
Zee2commented, Aug 4, 2021

Moving forward we will be trying to de-emphasize handler methods; for any we will still need, we will make sure that they are extensible + overridable in the relevant scenarios, in addition to following the current Unity patterns.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Why to Use Explicit Interface Implementation To Invoke ...
The explicitly implemented method/property then invoke another "protected virtual" method/property with same name. For example, public class ...
Read more >
How to implement interface events - C# Programming Guide
Learn how to implement interface events in a class. See code examples and view additional available resources.
Read more >
Distinguishing the Explicit and Implicit Interface ...
The explicitly implemented version calls the method that specifically belongs to IGetsMessage2 while the implicitly implemented version uses the ...
Read more >
Explicit Interface Implementation - C# Programming Guide
Explicit implementation creates a class member specific to one ... either as a public method, or as an explicit interface implementation.
Read more >
1161 – Explicit Implementation of Events in Interface
If you implement an event defined in an interface explicitly, you must use event accessor syntax, defining both add and remove accessors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found