Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a class from an interface

Is there a way in Visual Studio to right click an interface and 'Generate Class from Interface' so I end up with an empty class with all the properties and methods which the interface requires?

Kind of like Extract Interface, but backwards.

like image 856
mattdwen Avatar asked Aug 19 '10 22:08

mattdwen


People also ask

How do you create a class that implements an interface?

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.

Can we create class of interface?

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.

Can a class inherit from an interface?

Classes cannot inherit from an interface, since an interface is by definition empty: it only dictates the mandatory implementation of certain members. From the MSDN about interfaces: "An interface contains definitions for a group of related functionalities that a class or a struct can implement."

How do I extract an interface?

Place your cursor in the class name. Press Ctrl+R, then Ctrl+I. (Your keyboard shortcut may be different based on which profile you've selected.) Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Extract Interface from the Preview window popup.


2 Answers

Not quite what you're asking for, but if you create a class, and declare it as implementing your interface, you can right click on the interface and select "Implement Interface." This will add the appropriate methods to your class.

like image 79
Andy White Avatar answered Oct 22 '22 08:10

Andy White


I believe the answer before was for C#, but if you are using VB.NET all you need to do is type in the Implements IMyInterface the line below the class declaration and press <Enter>. It automatically generates all of the method and property signatures for you. You can also always go back to the Implements line and press again if any new methods or properties were added to the Interface and they too will be generated for you.

like image 24
atconway Avatar answered Oct 22 '22 09:10

atconway