coderushforroslyn-117650-refactoring-assistance-extract-interface.md
Extracts an interface from public members of a class.
You can use this refactoring in C# projects that contain file-scoped namespace declarations.
This refactoring allows you to create a new interface based on a selected class. It also makes this class implement this interface.
Available when the caret is in a class declaration statement. The caret should be in a class name and the class should contain at least one public member.
Place the caret in a class declaration. For example, in the “ProductInfo” class.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu. Select Extract Interface from the menu and press Enter.
In the code editor, you can type a new name for the generated interface or leave it as is. Press Enter in the code editor or click Apply in the Rename hint to apply the changes and close the hint.
The following code shows the result:
//FileName: ProductInfo.cs
using System;
namespace MyApp
{
class ProductInfo : IProductInfo
{
private int unitPrice;
private int count;
public int UnitPrice {
get { return unitPrice; }
set { unitPrice = value; }
}
public int Count {
get { return count; }
set { count = value; }
}
}
}
'FileName: ProductInfo.vb
Public Class ProductInfo
Implements IProductInfo
Private _unitPrice As Integer
Private _count As Integer
Public Property unitPrice() As Integer Implements IProductInfo.unitPrice
Get
Return _unitPrice
End Get
Set(ByVal value As Integer)
_unitPrice = value
End Set
End Property
Public Property count() As Integer Implements IProductInfo.count
Get
Return _count
End Get
Set(ByVal value As Integer)
_count = value
End Set
End Property
End Class
//FileName: IProductInfo.cs
namespace MyApp
{
public interface IProductInfo
{
int Count { get; set; }
int UnitPrice { get; set; }
}
}
'FileName: IProductInfo.vb
Public Interface IProductInfo
Property count() As Integer
Property unitPrice() As Integer
End Interface