Sandy Sandy - 3 months ago 45
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();


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.



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

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


enter image description here


enter image description here