Go to implemented method rather than interface
Asked Answered
P

6

11

Oftentimes when browsing code, I'll come across something like this:

public class Fruity
{
    private IOrange _Orange;

    public Fruity()
    {
        _Orange = new Orange() as IOrange;
    }

    public void PrepareFruit()
    {
        return _Orange.Peel();
    }
}

Great, so now I want to see how the Peel method is implemented. Right-clicking on the method gives me Go To Definition which takes me to the interface stub.

OK, strictly speaking, the definition is ascribed by the interface given that the private variable is defined in this way, but is there any way to just go to the implementation?

There is of course Find All References which is a scatter-gun approach of calls, interfaces and concretions. But the source of the implementation is obvious in this case so I should be able to jump to it I'd have thought...

Evidently, there can sometimes be ambiguity which is described nicely here:

Go to definition on concrete type

But surely, there should be a Go To Implementation option when the implementation is crystal clear.

Pleopod answered 5/4, 2013 at 9:43 Comment(7)
Might be worth downloading a trial version of ReSharper which has a "go to implementation" and a bunch of other stuff, I don't know how I ever functioned without it. I don't know how to do the same thing in the default IDEWhit
It isn't clear at all. Mismatched braces suggest you haven't posted the whole class Fruity, so how can we know you don't have a member function that sets _Orange to new MangoPretendingToBeAnOrange()?Striation
Seems like a duplicate of: #4663284 ?Actable
I second the notion of using Resharper. It's awesomely useful.Oneida
Visual Studio is not complete without ReSharper. When you use ReSharper's "Go to Implementation" and there are multiple implementations, it will provide a quick selection list. Other helpful commands in that regard are "Find Usages" or "Inspect Hierarchies"Reese
@hvd Apologies, missed the closing brace. I've corrected now...Pleopod
I've not been a fan of ReSharper in the past, but you're right - having reinstalled it, it does do this...Pleopod
G
7

If you double-click or highlight Peel and press CTRL+, you'll get the "navigate to symbol" window which will list the actual implementation, usually as the second item. It's about the fastest way of finding it without 3rd party tools. And unlike "find all references", it only shows the method definitions and not wherever it's called.

Gorki answered 5/4, 2013 at 9:53 Comment(2)
A Peel method could exist for all different fruits, and "Navigate To" doesn't limit itself to classes implementing IOrange as far as I know. +1 anyway, if method names are sensible, this shouldn't be a big problem.Striation
Indeed it's definitely not ideal, but it's probably the best option without 3rd party tools.Gorki
B
4

With Visual Studio >= 2015 you can jump to implementation with shortcut keys ctrl + F12

Babbler answered 28/8, 2017 at 14:11 Comment(0)
O
2

If you go to the Visual Studio menu "View" and select "Code Definition Window", when you click on .Peel() you might be shown the implementation of .Peel() (this doesn't always work, but try it and see).

Oneida answered 5/4, 2013 at 9:57 Comment(0)
J
1

Of course already exists in Visual Studio ! It is there since ever.

Right click on your code (Ex: property) an select "View Call Hierarchy". In Call Hierarchy window select the Implements folder.

There you are. Why Resharper ??? Of course is not that complex as go to implementation from resharper which allows direct interrogation on interface, but only a property, or a method from that interface should be enough. Ex:

public interface IModule
{
int Count { get; set; }
}

public class Module : Imodule
{ 
  public int Count {get; set;}
}

public class Module2 : Imodule
{ 
   public int Count {get; set;}  
}

Right click on the Count property (anywhere - inside the class or inside the interface) and select "View Call Hierarchy", should say which class implements it, and therefore the whole Interface.

At the beginning we all love Resharper, with time, we all hate it !

Jacquiline answered 20/9, 2013 at 21:46 Comment(1)
Yes, this is a little better than "Find All References", but it lacks the directness of "Go To Definition" which is the sort of thing I was looking for...Pleopod
M
1

Right Click on the method, and select Go to Implementation (it's the same as Ctrl + F12).

If you select Go to Definition, it opens the Interface.

Morry answered 17/5 at 20:22 Comment(0)
O
0

Visual Studio 2015 can do this with the "Go To Implementation" extension - https://visualstudiogallery.msdn.microsoft.com/0ed93222-83cd-4db3-92bc-a78909047156

If there are multiple implementations, it will show you a list so you can pick which to jump to.

Olio answered 4/11, 2015 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.