19

The functionality to extract an interface from a class (C#) seems to change in VS 2017. How can I do that in Visual Studio 2017.

Wheel Builder
  • 3,515
  • 5
  • 20
  • 32

5 Answers5

36

Right-click the class name and select "Quick Actions and Refactorings..." and then select "Extract interface..."

Note that you can also click on the class name and press Ctrl+. to bring up the same context menu.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Same as in VS2015, just slightly different name – Zac Faragher Apr 19 '17 at 01:56
  • 3
    For some reason, I'm not seeing this behavior. I used it all the time in 2015, but now all I see is "Generate overrides..." –  Oct 31 '17 at 20:51
  • 5
    Huh. Looks like I have to add a public property or method to a class before I can interface it... I guess that makes sense... but I don't like it. –  Oct 31 '17 at 21:02
5

With ReSharper installed, right click the class name: Refactor > Extract > Extract Interface...

Keep in mind that the Extract Interface... option is not even in the menu if the class is static. The same goes for the class members if they do not appear in the menu where you select what goes in the interface.

I completely forgot that you cannot have a static class with an interface or extends at all for that matter.

Heki
  • 926
  • 2
  • 15
  • 34
3

Extract an interface refactoring

Place your cursor in the class name.

Press Ctrl+R, then Ctrl+I.

https://learn.microsoft.com/en-us/visualstudio/ide/reference/extract-interface?view=vs-2019

2

I had the same problem with Visual Studio Community 2019 and I fixed it by disabling Resharper.

1

For some users the menu on right click on the class seems be deleted and now you can access only by right clicking on the class that you want to create the interface and than click on Quick actions and refactoring.

more info on this question