Extract Interface

Extracts an interface of public members from a class.

Purpose

This refactoring is a great first step for changing your code, because it uses interfaces instead of specific classes. It allows you to create a new interface based on the class you used previously, and makes that class implement that interface.

Availability

Available when the caret is on a class declaration statement. The caret should be on the class name and the class should have at least one public member.

Note
  • The resulting interface is declared right above the source class.
  • Rename is automatically invoked for the newly declared interface.

Usage

  1. Place the caret on a class declaration.

    Note

    The blinking cursor shows the caret's position at which the Refactoring is available.

    class ProductInfo
    {
        private int _unitPrice;
        private int _count;
        private int _UnitPrice;
        public int UnitPrice
        {
            get { return _UnitPrice; }
            set { _UnitPrice = value; }
        }
        public int Count
        {
            get { return _count; }
            set { _count = value; }
        }
    }
    
  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Extract Interface from the menu.

After execution, the Refactoring creates a new interface based on the class you used previously, and makes that class implement that interface.

internal interface MyInterface
{
    int UnitPrice { get; set; }
    int Count { get; set; }
}
class ProductInfo : IProductInfo
{
    private int _unitPrice;
    private int _count;
    private int _UnitPrice;
    public int UnitPrice
    {
        get { return _UnitPrice; }
        set { _UnitPrice = value; }
    }
    public int Count
    {
        get { return _count; }
        set { _count = value; }
    }
}