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

15

207

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 :)

Hammerfest answered 11/1, 2011 at 21:18 Comment(11)
How would it know which implementation you wanted to see? (I can view the type hierarchy to find all known implementations, but that may be a ReSharper feature... I'm not sure.)Rosannarosanne
@Jon Skeet: How about, if right-clicking an interface instance which is within scope, having an option to view the method associated with that particular instance?Vista
@supercat: Do you mean while debugging?Rosannarosanne
@Jon Skeet: Yeah. Not a universally-applicable situation, but one which would be handy often enough.Vista
ReSharper implements it by supplying a list of implementations to choose from where it can't detect the specific one. Nice feature and I do miss it as the "Find All References" brings back too much!Fug
possible duplicate of Visual Studio 2010 extension for Go To Interface Implementation?Eightieth
First time ever I have seen, Jon Skeet is saying "I'm not sure." for a C# question in SO.Apotheosis
@EAGER_STUDENT, LOL, but I hope you are aware that this is not really a C# question. It is an IDE question AND Jon's "not sure" comment was regarding a specific third party plugin, i.e. ReSharper.Germayne
@MuthuGanapathyNathan He was probably trying to look like he is human for once.Sclerous
Ctrl + F12 is shortcut in VS-2015Dicks
See Also: How do you find all implementations of an interface?Mcgehee
L
295

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).


Lawrenson answered 30/10, 2012 at 9:55 Comment(3)
Good workaround. I am sticking to that when I don't have ResharperGosport
I'm able to skip step 1. Just "View call hierarchy" (or shortcut Ctrl+K, Ctrl+T) right from where you are working and skip the interface all together. I'm in VS 2013.Somnus
Is there any hotkey to Peak the implementation? I tried ALT-CTRL-F12 with no success. (Peak is ALT-F12)Encasement
R
51

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".

Rasure answered 26/6, 2014 at 14:54 Comment(5)
You can use Alt+End if you're using resharper.Stamen
Didn't work for me. I am using VS Express 2013 for Web.Kike
I find this as the simplest soln, without taking your handsoff from keyboard. Thanks RolfVaricocele
This is the quickest solution possible.Laplace
@Stamen You can also set this to be Alt+End without R# by customising the keyboard shortcut for command Edit.NavigateTo :)Toogood
T
18

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)

Timeous answered 7/8, 2013 at 2:3 Comment(1)
The resharper trial has expired at work, tried this extension and it rocks! thanks dudeEmetine
A
15

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.

Actinism answered 11/1, 2011 at 21:43 Comment(6)
This does not give the imlpementation.Crankcase
@Crankcase actually, it does. It's not the most convenient way, but the implementations will be mixed in with all the other references.Drexler
That's the only thing I do right now but it's tiresome above all as I don't have good eyes :)Hammerfest
An alternative to finding symbols is the call hierarchy. It has the methods grouped in a folder structure: Calls To '...', Calls From '...' and Implements '...'Actinism
I marked your answer as right one though it's not what I wanted but it's the fault of Microsoft not yours :)Hammerfest
Shortcut-key for "Find All References" is Shift + F12.Vulcanite
V
14

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.

Virtual answered 13/11, 2018 at 4:30 Comment(1)
this shortcut works fine at vscode with typescript.Sandell
C
8

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.
Crankcase answered 11/1, 2011 at 21:51 Comment(2)
I'm fairly sure Architecture Explorer is Ultimate edition only, but this will work.Cannabis
Thanks quite good I thumbed up but in this context I want be quick to change my code.Hammerfest
A
8

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

Alexina answered 18/12, 2015 at 10:30 Comment(0)
W
6

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.

Wollastonite answered 8/11, 2012 at 6:27 Comment(4)
Didn't notice that. Thanks. <br/> Now if there was only a way to get the solution explorer to open it's tree to the currently displayed file so I don't have first bring up the tooltip of the editor to get the file's path so as to derive then navigate through the solution explorer to the source file, where I can the determine the list of implemented-by's for the interface... Source code navigation should become a more important feature I think.Lissner
-- Just discovered the "Ctrl + ], S" command which expands the solution explorer tree to the current edit document (sync with active document). The resultant file node may then be expanded to expose the contained types which in turn may be right clicked to access the context menu containing the 'Derived Types' command. Nice.Lissner
@George, you can also have Visual Studio ALWAYS track the current open file in the solution explorer via Tools -> Options -> Projects and Solutions -> General -> "Track active item in solution explorer"Lawrenson
VS2012 Premium has this feature too. In VS2015 Enterprise, this 'Derived Types' feature has been split to two features 'Derived Types' & 'Implemented By', even nicer :)Vetavetch
C
5

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

Cyclic answered 27/1, 2015 at 10:54 Comment(0)
V
4

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.

Veliavelick answered 11/1, 2011 at 21:31 Comment(5)
…and if there is only one implementation, R# will take you straight there without further ado.Milkwort
It's not free, but neither is Visual Studio. How much is your time worth? :)Ygerne
It's not that I don't want to pay, It's on the principle: this is quite basic common sense feature, it is shamefull you have to pay once more when you already paid for VS :)Hammerfest
@Hammerfest While I agree with you, not buying the product that does exactly the feature you want in order to "punish" a third party for not providing it out of the box, is cutting off your nose to spite your face.Sphenic
Resharper provides this without having to show all usages. You can select which implementation you want to go to.Resound
F
4

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".

Fleck answered 13/1, 2016 at 21:25 Comment(0)
I
2

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.

Isoagglutinin answered 18/6, 2015 at 18:54 Comment(0)
O
1

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.

Odaodab answered 11/1, 2011 at 21:26 Comment(0)
A
1

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++.

Aerothermodynamics answered 11/1, 2011 at 21:56 Comment(0)
L
0

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.

Lissner answered 24/2, 2013 at 2:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.