Sandy Sandy - 12 days ago 7
C# Question

Generate Interface from existing class

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 Interface from it. Like, MyClass is implementing 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 any other simple method to follow to accomplish this? If not clear, leave a comment.

Thanks.

Answer

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