70

Suppose you have an interface defined in C#. What is the easiest method to find all classes that provide an implementation of the interface?

The brute force method would be to use "Find References" in Visual Studio and manually look through the results to separate out the usages from the implementations, but for an interface in a large codebase that is heavily referenced with relatively few implementations, this can be time consuming and error prone.

In Java, running javadoc on the codebase (using the -private option to include private classes) would generate a documentation page for the interface (e.g. Comparable) that includes all implementing classes for the interface as well as any subinterfaces (though it doesn't include implementing classes of the subinterfaces, these are relatively easy to determine by drilling down into the listed subinterfaces). It's this functionality that I'm looking for but with C# and Visual Studio.

akasoggybunz
  • 189
  • 5
  • 21
iammichael
  • 8,167
  • 3
  • 30
  • 41

12 Answers12

105

You can right click a method name (definition in interface or implementation in other class) and choose View Call Hierarchy. In Call Hierarchy window there is "Implements" folder where you can find all locations of the interface method implementation.

phoose
  • 1,059
  • 2
  • 7
  • 2
  • I see no such menu option (in VS 2008) – iammichael May 05 '11 at 15:42
  • 1
    Available since VS2010 it seems - I just tried this in VS2013 Express and it's there too - very nice to have an alternative to F12, which doesn't always go where you expect in code that uses dependency injection. – J c Feb 19 '14 at 05:41
  • 10
    this should be the Accepted Answer as it (a) does the job, and (b) does not rely on [paid] third party tools – NekojiruSou Jul 10 '14 at 16:37
36

(Edit based on comment...)

If you have ReSharper installed:

In Visual Studio, right click on the type name and choose "Go to Inheritor". Alternatively, select the type name, then go to ReSharper/View/Type Hierarchy to open up a new tab. (The menu will show you the keyboard shortcut - this can vary, which is why I explained how to find it :)

If you don't have ReSharper:

  • You can use Reflector, which is able to show you all the type hierarchy very easily - just under the type name are expandable items for base types and derived types. Similar tools are available such as ILSpy and dotPeek.
  • Buy ReSharper - it's a great tool :)
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • ReSharper is already on my list of requests; I figured it probably had the capability, but alas, I don't yet have it. Reflector allowed me to get the answer to my specific search, though it was a bit of a pain to find and add all the assemblies that reference the one with the interface definition! – iammichael Mar 06 '09 at 21:21
  • Reflector is not free – beruic Mar 07 '16 at 13:57
  • 1
    @beruic: Not any more - it was back when I wrote this, I think :) Will add some free options... – Jon Skeet Mar 07 '16 at 14:06
  • @JonSkeet does Resharper browse even the third party DLLs ?. for example, I want to see the implementations for an interface in some NuGet package and the implementations are in that package. – Wahid Bitar Apr 15 '16 at 18:06
  • 1
    @WahidBitar: I would expect it to do that within assemblies in your project. I suggest you give it a try :) – Jon Skeet Apr 15 '16 at 18:15
  • Yup found it. you can get it from Inspect -> Hierarchies. Thanks a lot my master ^_^. BTW you always inspire me. thank you :) – Wahid Bitar Apr 15 '16 at 18:20
  • Note that ILSpy is free, and includes a Visual Studio add-in with which you can navigate to the interface (assuming it's your own code) and then view the call hierarchy. – yoyo Jan 19 '17 at 01:39
26

Put the cursor to class or interface type and

CTRL + F12

Bahtiyar Özdere
  • 1,508
  • 12
  • 18
15

With Visual Studio 2010+

Right click a member method and choose view call hierarchy. Expand the Implements folder. This lists all the types that implement the interface the method belongs to.

enter image description here

With Resharper 7

Right Click the interface > Navigate To > Derived Symbols. The symbols listed in bold derive directly from the interface. Non-bold symbols derive from a superclass.

enter image description here

P.Brian.Mackey
  • 39,360
  • 59
  • 210
  • 327
10

For those using Visual Studio 2015, there is this awesome extension called Go To Implementation. Give it a try.

Once you've installed the extension, you can just right click at any occurrences of the interface (e.g. IUrlShortener) and click on Go To Implementation menu. If you only have one class that implements the interface, clicking the menu will bring you directly to the class. If you have more than one class that implements the interface, it will list all the classes.

Nik A.
  • 373
  • 3
  • 13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – ForceMagic Aug 30 '15 at 15:41
  • @ForceMagic what "essential parts of the answer"? It's a link to a downloadable extension. I'll add more explanations to the answer though, thanks. – Nik A. Aug 30 '15 at 16:59
  • Finally! Though I still can't believe this crucial feature is missing in VS. – wziska Oct 02 '15 at 10:50
  • 3
    @wziska As you wished, this feature is now implemented in [Visual Studio 2015 Update 1 RC](http://blogs.msdn.com/b/visualstudio/archive/2015/10/29/visual-studio-update-1-rc.aspx) :) – Nik A. Nov 06 '15 at 16:00
8

For those using Visual Studio 2015, install Visual Studio 2015 Update 1 RC. From the Visual Studio blog:

Today we released Visual Studio 2015 Update 1 RC, which builds on the Update 1 CTP we released three weeks ago. In addition to the features introduced in the CTP as described here, the Release Candidate includes the following:

  • Go To Implementation. The feature many of you have been waiting for: just right-click on an interface or abstract method and select this command to navigate to the implementation.
Community
  • 1
  • 1
Nate Cook
  • 7,577
  • 5
  • 44
  • 36
2

You could do a regular expression search for the interface.

:(\ *[^},]+,)*\ *IMyInterfaceName

CTRL+SHIFT+F launches the following window:

Visual Studio Screenshot

Kees C. Bakker
  • 28,682
  • 24
  • 104
  • 188
2

I prefer the "Navigate To..." option. With your cursor on the function call, try the following:

Shortcut Key:

  1. Ctrl+, (Ctrl+comma)

Menu:

  1. Edit Menu
  2. Click "Navigate To..."

Benefits:

  • Doesn't show all references like "Find All References"
  • Shows the "type" of the implementation so it will note which is your interface
Tony L.
  • 13,638
  • 8
  • 61
  • 63
2

I don't think this functionality is inbuilt into VS but IIRC Resharper has this.

Mohit Chakraborty
  • 1,111
  • 7
  • 8
1

Use Shift + F12 to show all references, including the definitions.

Akira Yamamoto
  • 4,196
  • 3
  • 39
  • 41
0

If you use resharper ALT + END shortcut might help to find all Inheritors.

Canavar
  • 46,286
  • 17
  • 83
  • 120
0

I've heard tell (no experience myself) that doxygen is to .Net as as javadoc is to java.

albert
  • 5,966
  • 3
  • 13
  • 29
overslacked
  • 4,057
  • 22
  • 28