Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to implemented method rather than interface

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.

like image 602
Robbie Dee Avatar asked Apr 05 '13 09:04

Robbie Dee


People also ask

What is go to implementation?

Go to Implementation Last modified: 28 October 2021. ReSharper | Navigate | Go to Implementation. Ctrl+F12 ( ReSharper_GotoImplementations ) This command helps you find actual implementations of types and members — in other words, to locate the source code they execute.

Can we leave any method of the interface from implementing in the class?

If you don't implement all methods of your interface, than you destroy the entire purpose of an interface.

How do you open the implementation method in Visual Studio?

Navigate to implementation of a type or a type memberChoose Navigate | Go To Implementation in the main menu, press Ctrl+F12 , or click the symbol while holding Ctrl+Alt keys. Note that in Visual Studio 2017 and later, the Ctrl+Alt -click is used for adding multiple carets.

How can we avoid implementing all methods interface?

Declare the missing methods abstract in your class. This forces you to declare your class abstract and, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects.


2 Answers

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.

like image 63
PhonicUK Avatar answered Sep 23 '22 13:09

PhonicUK


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

like image 28
Matthew Watson Avatar answered Sep 25 '22 13:09

Matthew Watson