79

I have a class as:

Class MyClass
{
   public MyClass { ... }
   public string Name { get { ... } }
   public int IdNumber { get { ... } set { ... } }
   public void GenerateNme {...}
}

It is just a sample class. I wish to generate an Interface from it. Like, MyClass is implementing the IMyClass interface. I wish the output to be

public Interface IMyClass
{
   string Name { get; }

   int IdNumber { get; set; }

   void GenerateNumber();
}

and

MyClass : IMyClass
{

}

It can be done manually, but I was just curious to know, is there another simple method to follow to accomplish this?

1
  • Only by inspection: The syntax highlighter doesn't seem to like the first and the last part. Shouldn't it be "class" (lower case) and some kind of qualifier before "MyClass", respectively? Or is the syntax highlighter just weird? Or is the last part actually correct? May 19 at 15:42

8 Answers 8

146

Yes, you can extract an interface from a class using Visual Studio:

Inside the target class file: Right Click > Refactor > Extract Interface...

Example

enter image description here

then

enter image description here

6
  • As far as I've been able to find out, this is not possible for VB.NET in any version of Visual Studio prior to VS 2015 when it was added.
    – SteveCinq
    Feb 1, 2017 at 16:17
  • One other thing that had me confused (seems obvious now) - the class you are extracting an interface for needs at least one public method... I had to change all internal methods to public.
    – jwill212
    Sep 10, 2018 at 14:24
  • 5
    Refactor isn't available in VS2017 Sep 27, 2018 at 19:38
  • 6
    in VS2017 click on the class name in code, right click, select 'Quick actions and refactorings', select 'Extract Interface'
    – ywwy
    Dec 19, 2019 at 10:21
  • 3
    in (VS 2019) : unfortunately this works only for first time. Once you generate the interface and add few new methods in class , then you can not automatically add this new methods via some options , even if you delete the interface & its .cs file again and try to create new inteface , but somehow visual studio keeps generating blank inteface , looks like it saved this infor somewhere that iterface has been created once earlier. Jan 17, 2020 at 10:24
60

In Visual Studio 2015/2017/2019/2022, this is under the Quick Actions menu (Ctrl+ period .)

Be sure to put the cursor somewhere in the class name you want to extract the interface from. Otherwise it shows "no quick actions available here".

Note: this is only possible if you can actually extract an interface. For example if your class only has static methods this will not work.

2
  • 1
    Strange, my class does not have any static members at all but I still cannot extract it.
    – Essej
    Jul 3, 2020 at 6:19
  • Thank you! I've been struggling with this for months! I can get to the Interface UI, but when I click OK, nothing has happened. Having the cursor on the class name did the trick for me.
    – Rae
    Feb 1 at 17:05
14

In Visual Studio 2010, you can right-click MyClass and choose Refactor, `Extract Interface..." (Ctrl+R, I). This gives you a window to check the members to be extracted.

11

In the latest versions of Visual Studio (2019 & 2022) the menu item has been renamed from the accepted answer. The complete list of how to now get to the interface popup is as follows (only the first listed has changed from earlier versions):

  • Right click on the name of the class and select the Quick Actions and Refactorings... menu option (not Refactor as in earlier versions). Another menu will then appear, select Extract interface....

    Quick Actions and Refactorings popup menu

  • Have the cursor in the name of the class and then hold down Ctrl and press . then select the Extract interface... menu option.

    Extract interface popup menu

  • Go to the Edit menu item then Refactor and Extract Interface (you do not need to have the class name selected for this, the operation will be performed on the class you have open)

    Extract interface full menu

  • Hold down Ctrl then press R and I in close succession (again the operation will be performed on the class you have open)

Performing any of the four above actions will bring up the Extract Interface window (this is largely unchanged from earlier versions, although you can choose to add the interface to your current file)

Extract Interface window

4

In Visual Studio 2015, click cursor in or right click on the class name, then select Quick Actions (or press Ctrl-.) and the 'Extract Interface' option shows.

1
  • Cool edit @Stijn, I'll have to remember that: <kbd>Ctrl</kbd> Feb 16, 2016 at 16:32
2

Please take note. If your class has a static in it you won't be able to extract to interface, so remove that first before you extract to interface

1
  • To clarify on the above static methods won't be extracted but any non static methods will be.
    – d219
    Jan 11 at 9:38
1

In the Refactor menu of Visual Studio, there is an "extract interface" option that does exactly what you describe.

0

Ctrl+. was popping up 'generating overrides...' and nothing was happening beyond that so I searched 'refactor' in the quick launch search box. Results had Edit -> Refactor -> Extract Interface (Ctrl+R, Ctrl+I) option.

Hoping, this tip can help someone else too. I am using VS 2017 EE.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.