0

What is the use of calling interface methods within the class that has implemented it instead of implementing the the particular method itself like below -

public class MyController: Controller, IActionFilter {
  public ActionResult Index(Customer obj) {
    return View(obj);
  }
  void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext) 
  {
    Trace.WriteLine("Action Executed");
  }
  void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) {
    Trace.WriteLine("Action is executing");
  }
}

What does calling out methods like this way -> "IActionFilter.OnActionExecuted" achieve?

Khongor Bayarsaikhan
  • 1,644
  • 1
  • 16
  • 19
user9057272
  • 367
  • 2
  • 5
  • 17

1 Answers1

3

void IActionFilter.OnActionExecuted is an explicit implementation of that interface method.

You can only call the OnActionExecuted method on a MyController instance if you cast your instance to IActionFilter.

However if you make an implicit implementation like:

public void OnActionExecuted(ActionExecutedContext filterContext)

you will be able to call OnActionExecuted on a MyController instance without casting it as IActionFilter

Khongor Bayarsaikhan
  • 1,644
  • 1
  • 16
  • 19
  • This helped. Also, looked up Explicit Interface Implementation here (for others like me) - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation – user9057272 Oct 04 '20 at 06:39