Reputation: 11687
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?
Upvotes: 84
Views: 73760
Reputation: 2834
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....
Have the cursor in the name of the class and then hold down Ctrl and press . then select the Extract interface... menu option.
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)
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)
Upvotes: 13
Reputation: 21881
In the Refactor menu of Visual Studio, there is an "extract interface" option that does exactly what you describe.
Upvotes: 1
Reputation: 87
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
Upvotes: 2
Reputation: 4534
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.
Upvotes: 60
Reputation: 16195
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.
Upvotes: 0
Reputation: 12513
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.
Upvotes: 14
Reputation: 9558
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.
Upvotes: 4
Reputation: 48975
Yes, you can extract an interface from a class using Visual Studio:
Inside the target class file: Right Click > Refactor > Extract Interface...
Example
then
Upvotes: 150