20

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.

3 Answers 3

34

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I found that in VS 2013, you have to right click on the interface name, not the class name.
In VS2015, you create an empty class which inherits the interface. Then you click on the interface name and hit "Ctl + ." (or click on the lightbulb to the left of the class name) and then select "Implement Interface" from that menu.
Any way to generate an interface that matches a class? E.g. implement class in an interface? I find myself making small updates and it's a nuisance to keep updating the interface manually
2

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.

Comments

1

To specifically address this question, yes, there is. Caveat, I'm using C#, working with the Visual Studio 2013 Ultimate Edition.

Create an empty class, like "Class1":

class Class1
{
}

Then append an interface name, such as IObjectBase (which I'll continue to use for the remainder of this post), by using the ":", as in the following example:

class Class1:IObjectBase 

When you do, there will be a short, thick, white underline, under the first letter of your interface name "I". Mouse-over it. There are a number of options which may appear. You're looking for a small icon which looks like a page with a dog-eared, top-right corner as you hover over the small, thick white line, with the tooltip of "Options to implement interface". Click on the drop-down arrow to its right.

You'll be presented with two menu item options: "Implement interface 'IObjectBase'" and "Explicitly implement interface 'IObjectBase'". From there the choice is yours.

If you're looking for guidance regarding the two options, I'd suggest reading the following articles, I think each covers the topic well, yet from different viewpoints:

Regards,

HALAR

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.