How do you quickly find the implementation(s) of an interface's method? [duplicate]
Asked Answered
S

6

72

Is there a quick way to find all of the implementations of, not references to, an interface's method/property/etc? Here's some sample code:

public class SomeClass : IBaseClass
{
  public Int32 GetInt()
  {
     return 1;
  }
}

public interface IBaseClass
{
  public Int32 GetInt();
}

public class SomeOtherClass
{
  IBaseClass _someClass;
  private TestMethod()
  {
    _someClass = new SomeClass();
    _someClass.GetInt();
  }
}

I want to quickly get to SomeClass.GetInt() while reviewing SomeOtherClass.TestMethod(). If I right click on _someClass.GetInt() and click 'Go To Definition', it takes me to the interface. If I click 'Find All References', I could potentially see a list of all uses ... not just the classes that implement the GetInt() method.

Is there a faster way to find this? Any tips from other developers? We are using D.I. for most of our dependencies, which means that tracing through deeply nested code takes forever.

Stantonstanway answered 11/8, 2009 at 16:43 Comment(1)
Use Shift + F12 to show all references, including the definitions.Eviscerate
F
122

Since I don't like to use the mouse while coding, I usually

  • move the cursor over the method
  • type ctrl+k clrl+t to open the Call Hierarchy window
  • move down to Implements node.
  • type Return to go to the selected implementation

AFAIK this is the quickest way to find the implementation of a method, without ReSharper.

(By the way: you can use the same system to move from a class method implementation to the corresponding interface declaration: just select the root)

Fourfold answered 24/11, 2011 at 16:28 Comment(5)
You need VS2010 for that, right?Unbraid
@Arialdo Martini: Absolutely perfect !Ricercare
I don't think you need the second Ctrl. i.e Ctrl-K, T should workRhotacism
Just to clarify; to see the 'Implements' node, the Call Hierarchy needs to be invoked from the member definition in the interface.Manhattan
How about in VS for Mac?Lovell
D
24

Alt-End will do this in ReSharper, I believe.

Donor answered 11/8, 2009 at 16:44 Comment(6)
Ouch, $349 per developer. That's what we paid for VS2008 pro.Stantonstanway
You didn't ask for a sales pitch for ReSharper, and what you're earning is none of our business, but consider what kind of money you're making doing what you're doing, and then consider how quickly you'd recoup the license fee. If the numbers are right, $350 is worth it. To give you some perspective, I'll claim that, after 1 month of actually slowing myself enough to learn ReSharper's ins and outs (and, to be sure, I'm still learning some of them) while I work, the tool increases my productivity as a code author by 30% at LEAST (it's probably higher, frankly, but I don't want to exaggerate).Donor
I agree, any company that balks at resharpers price (the C# only edition is even cheaper) is an idiot since the time you waste without costs a company $1000s. I also agree with lance's view that the cost of resharper is easily recouped in a month.Yesteryear
I've used it some in the past. While I found it useful for refactoring (which I don't do often), I didn't find it making me much more productive. I'll admit that I didn't spend a lot of time learning the "ins and out", but it's hard for me to justify the expense when our only problem with VS.NET is finding implemented interfaces.Stantonstanway
Agreed! I don't care how much money you're making -- you'd have to use the "Go To Implementation" feature quite a lot to recoup the license fee from that feature alone! :)Donor
Also, Ctrl-Alt click will now take you to implementation: https://mcmap.net/q/131233/-resharper-is-it-possible-to-go-to-method-39-s-implementation-on-ctrl-click-instead-of-going-to-declarationDonor
S
9

Without ReSharper the best way to do this is:

Find in files (Ctrl+Shift+F) Find What: "class*ISomeClass" Find Options: "Use Wildcards"

This will find all implementation and than you can search for your function in a concrete implementation.

Selby answered 19/5, 2010 at 12:32 Comment(1)
Feels weird seeing ISomeClass, we usually start interfaces with 'I' rather than classes... but that's just usMetaphase
A
3

you could try going with this plugin:

http://blog.rthand.com/post/2010/01/18/Meet-e2809cGo-To-Implementatore2809d-DXCore-plugin-for-Visual-Studio.aspx

Arleen answered 9/6, 2010 at 11:56 Comment(4)
This plugin is for CodeRush and it seems that free version of CodeRush is discontinued.Unbraid
CodeRush Xpress seems very alive devexpress.com/Products/Visual_Studio_Add-in/CodeRushXForehand
CodeRush Xpress doesn't support vs2013Battery
the link isn't availableJaynajayne
S
2

If your interface is in the same library as your concrete model that you'll typically want to navigate to, you could add a 'using alias' to the concrete object. 'Usings' are helpers anyhow, and this helps.

using System;
using PrimaryImplementation = YourNamespace.YourConcreteObject;


public interface IYourInterface{

}

When you're taken to the interface, you have a quick (and dirty) way of getting to the primary implementation of your interface. F12->F12!

Stopover answered 6/3, 2013 at 16:18 Comment(0)
C
1

R# has a Go to Implementation option on the pop-up menu, which is really handy for that.

Conscious answered 11/8, 2009 at 16:53 Comment(5)
Sounds great, nothing in VS that you know of?Stantonstanway
Not beyond Find References which you already mention. If you don't know R# I really encourage you to check it out. In addition to features like this it has useful coding tip and a really powerful set of refactoring features.Conscious
Isn't R# a completely different programming language, or there an R# IDE as well? We are building an ASP.NET MVC app ...Stantonstanway
R# is Resharper. A plug-in for Visual Studio. It adds a bunch of useful commands.Conscious
Ah! I had just googled R# and came up with this ( sourceforge.net/projects/r-sharp ). I'm obviously not down with lingo =)Stantonstanway

© 2022 - 2024 — McMap. All rights reserved.