Is there a way to navigate to real implementation of method behind an interface?
Solution 1
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
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
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
(source: microsoft.com)
Solution 4
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
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.
Related videos on Youtube
Comments
-
user310291 about 3 years
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 :)
-
Jon Skeet over 13 yearsHow 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.)
-
supercat over 13 years@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?
-
Jon Skeet over 13 years@supercat: Do you mean while debugging?
-
supercat over 13 years@Jon Skeet: Yeah. Not a universally-applicable situation, but one which would be handy often enough.
-
Fenton over 11 yearsReSharper 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!
-
Peter O. over 11 yearspossible duplicate of Visual Studio 2010 extension for Go To Interface Implementation?
-
Muthu Ganapathy Nathan almost 11 yearsFirst time ever I have seen, Jon Skeet is saying "I'm not sure." for a C# question in SO.
-
Andrew Steitz about 10 years@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.
-
Vort3x over 7 years@MuthuGanapathyNathan He was probably trying to look like he is human for once.
-
Kartik Goyal over 7 yearsCtrl + F12 is shortcut in VS-2015
-
KyleMit about 3 years
-
-
Jay over 13 years…and if there is only one implementation, R# will take you straight there without further ado.
-
Robaticus over 13 yearsThis does not give the imlpementation.
-
JonDrnek over 13 years@Robaticus actually, it does. It's not the most convenient way, but the implementations will be mixed in with all the other references.
-
Richard over 13 yearsI'm fairly sure Architecture Explorer is Ultimate edition only, but this will work.
-
user310291 over 13 yearsThanks quite good I thumbed up but in this context I want be quick to change my code.
-
user310291 over 13 yearsThat's the only thing I do right now but it's tiresome above all as I don't have good eyes :)
-
Jim Arnold over 13 yearsIt's not free, but neither is Visual Studio. How much is your time worth? :)
-
Phil Carson over 13 yearsAn alternative to finding symbols is the call hierarchy. It has the methods grouped in a folder structure: Calls To '...', Calls From '...' and Implements '...'
-
user310291 over 13 yearsIt'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 :)
-
user310291 over 13 yearsI marked your answer as right one though it's not what I wanted but it's the fault of Microsoft not yours :)
-
TarkaDaal over 11 years@user310291 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.
-
George about 11 yearsDidn'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.
-
George about 11 years-- 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.
-
Roman about 11 yearsGood workaround. I am sticking to that when I don't have Resharper
-
MajorRefactoring about 11 years@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"
-
IsmailS over 9 yearsShortcut-key for "Find All References" is
Shift + F12
. -
brechtvhb over 9 yearsYou can use Alt+End if you're using resharper.
-
Giorgio Minardi over 9 yearsThe resharper trial has expired at work, tried this extension and it rocks! thanks dude
-
Akira Yamamoto over 9 yearsDidn't work for me. I am using VS Express 2013 for Web.
-
Michael Joyce over 9 yearsResharper provides this without having to show all usages. You can select which implementation you want to go to.
-
Jp Vinjamoori over 9 yearsI find this as the simplest soln, without taking your handsoff from keyboard. Thanks Rolf
-
mbudnik about 9 yearsThis is the quickest solution possible.
-
Tony L. about 9 yearsI'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.
-
demoncodemonkey over 8 years@brechtvhb You can also set this to be Alt+End without R# by customising the keyboard shortcut for command
Edit.NavigateTo
:) -
cateyes over 8 yearsVS2012 Premium has this feature too. In VS2015 Enterprise, this 'Derived Types' feature has been split to two features 'Derived Types' & 'Implemented By', even nicer :)
-
Levi Fuller over 7 yearsIs there any hotkey to Peak the implementation? I tried ALT-CTRL-F12 with no success. (Peak is ALT-F12)
-
Zuhair Taha over 4 yearsthis shortcut works fine at vscode with typescript.