Is there a way to navigate to real implementation of method behind an interface?

C#.Netvb.netVisual Studio

C# Problem Overview


In Visual Studio, when you right-click a method call, you go to the implementation of that method inside a class except if you access this method through an interface: in that case you go to the interface method not to the actual implementation.

Is there a way / tips (key shortcut or anything) to access this actual implementation ? Otherwise you are stuck to add some comment just to remember where you did implement it that's really not productive and error prone !

Update: interesting answers but I'm not really satisfied because all are cumbersome. I will give a precise example:

IInterface iInterface = someObject;                        
iInterface.someMethod();

Actually if Visual Studio was just a little bit clever to look just one line above the method call it would see where's the real object is. And that would save me a lot of keystrokes and avoid to use "find all references" and then scan the lines with my tired eyes to see which line contain the right one :)

C# Solutions


Solution 1 - C#

I do the following:

  1. Right click on the method and click "View call hierarchy" (or shortcut Ctrl+K, Ctrl+T)

  2. Expand the "Implements x" folder which will then show you all the implementations of that method. Click on one to go there.

Relatively quick and easy. Annoyingly though there doesn't seem to be an equivalent for the interface itself.


update: as of Visual Studio 2015 update 1, right click on a method and select go to implementation. You can also map it to keyboard shortcut via Tools > Options > Environment > Keyboard and search for Edit.GoToImplementation command. The default shortcut is Ctrl+F12. (F12 will navigate to the interface).


Solution 2 - C#

With VS2013 one can place cursor over the method, and use Navigate To... (CTRL+,), and it will display all locations where the name is declared. Doesn't work well if different interfaces uses the same method names.

With VS2015 Update 1 there is now a new shortcut called "Go To Implementation".

Solution 3 - C#

I created a free extension for Visual Studio 2010 and Visual Studio 2012 called Inheritance Margin to provide this specific feature, as well as give a clear indication when a method implements an interface method due to a signature match. In the current version, you can right click on any glyph to get a menu of items to navigate to.

Inheritance Margin - Visual Studio Gallery

Screenshot
(source: microsoft.com)

Solution 4 - C#

Right-click then "Find All References".

This will display the line of code for all the places where the method is used including the interface declaration and implementations of the interface method. You can then click on the line to jump to the code location.

Solution 5 - C#

Update for Visual Studio 2015 - Release 1

You can use Edit.GoToImplementation with Ctrl + F12

It will lead you to implementation just like it will lead you to non interface methods using F12.

Solution 6 - C#

Depending on the version of Visual Studio that you have, I'll say conditionally "yes."

I'm currently operating on Ultimate, and don't have other versions to verify this. That said, within Ultimate, you can use the Architecture Explorer to find implementations. It's a little more involved than the right click method, but here's how it works.

  • Go to View->Architecture Explorer (or CTRL-W, N)
  • Click on Class View and find the namespace that contains your interface in the list that opens.
  • To the right of the namespace list, is a vertical button that says Types. Click this
  • Select Interfaces
  • Choose your interface from the list
  • A vertical button that says Members will then appear to the right. Click that button
  • Choose Implemented by (under Inbound Navigation) and that will provide a list of implementations of your interface.
  • Double clicking on the implementation will take you to the class.

Solution 7 - C#

Visual Studio 2015 Update 1 (released December 2015) has now added a right click 'Go To Implementation' feature as standard.

Solution 8 - C#

Within 2012 Ultimate, you can search the interface in solution explorer. Right-click the interface and choose "Derived Types", the implemented classes will shown in solution explorer. Not sure if it works in express.

Solution 9 - C#

For people who use Resharper, pressing CTRL + F12 will make you go directly to the class method!

Solution 10 - C#

This is not possible. What you are describing would make sense only if the interface would be limited to 1 implementation.

Consider this example:

interface IWrite 
{ 
 void Write(); 
}
class A : IWrite { 
  public void Write() { Console.WriteLine("A"); } 
}
class B : IWrite { 
  public void Write() { Console.WriteLine("B"); } 
}
class X : IWrite { 
  private readonly string _x;
  public X(string x) {
    _x = x;
  } 
  public void Write() { Console.WriteLine(_x); } 
}

class UseIWrite()
{
  public void Use(IWrite iwrite) 
  {
     iwrite.Write();
  }
}

If you use go to implementation of Write in UseIWrite, it takes you to the declaration of the interface, because at that point any of the IWrite implementations could be passed into the method.

Thankfully some tools like for example ReSharper offer you to find all usages of the method, so you can easily navigate to the desired implementation.

Solution 11 - C#

Visual Studio with Update 1 now implements this feature! Just right click the member and you should see "Go to implementation" right under "Go to definition".

Solution 12 - C#

Check out this new free Visual Studio 2013 Extension - Implementator. It adds option "Go to implementation" to Visual Studio editor context menu.

It is not so reliably and fast as Resharper, but does it's job just fine.

Solution 13 - C#

Going to the declaration will open up the interface method. Going to the implementation, will take you, or list for you, the classes that implement the code for that interface method (not the interface method itself).

Update

As Jon Skeet pointed out in the comments (and I missed before answering), the feature I've described might be a ReSharper feature...not a Visual Studio one.

Solution 14 - C#

Visual Studio can only tell you where it is referenced, but this may be too coarse.

There are other tools that can tell you more about the structure of your application, including which class implements which interface, which method overrules which other method, and so on. I personally prefer using Understand For C/C++.

Solution 15 - C#

No. This is not possible, despite the elaborate in memory code structure maintained by VS, code navigation is rather...stingy.

It seems the only alternative is a global search with "Find All Reference" followed by a manual search of the resulting list to exclude typed value declarations, conversions, etc. or the use of other tools within VS other than the code editor. All rather complex and disappointing.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser310291View Question on Stackoverflow
Solution 1 - C#MajorRefactoringView Answer on Stackoverflow
Solution 2 - C#Rolf KristensenView Answer on Stackoverflow
Solution 3 - C#Sam HarwellView Answer on Stackoverflow
Solution 4 - C#Phil CarsonView Answer on Stackoverflow
Solution 5 - C#Akbar BadhushaView Answer on Stackoverflow
Solution 6 - C#RobaticusView Answer on Stackoverflow
Solution 7 - C#PaulGView Answer on Stackoverflow
Solution 8 - C#Jack Liu ShuruiView Answer on Stackoverflow
Solution 9 - C#MaximcView Answer on Stackoverflow
Solution 10 - C#m0saView Answer on Stackoverflow
Solution 11 - C#Jason SteeleView Answer on Stackoverflow
Solution 12 - C#YuraView Answer on Stackoverflow
Solution 13 - C#Jamie DixonView Answer on Stackoverflow
Solution 14 - C#PatrickView Answer on Stackoverflow
Solution 15 - C#GeorgeView Answer on Stackoverflow