Export (0) Print
Expand All

Extract Interface Refactoring (C#)

Extract Interface is a refactoring operation that provides an easy way to create a new interface with members that originate from an existing class, struct, or interface.

When several clients use the same subset of members from a class, struct, or interface, or when multiple classes, structs, or interfaces have a subset of members in common, it can be useful to embody the subset of members in an interface. For more information about using interfaces, see Interfaces (C# Programming Guide).

Extract Interface generates an interface in a new file and positions the cursor at the beginning of the new file. You can specify which members to extract into the new interface, the name of the new interface, and the name of the generated file using the Extract Interface dialog box.

To use Extract Interface

  1. Create a console application named ExtractInterface, and then replace Program with the following code

    // Invoke Extract Interface on ProtoA.
    // Note:  the extracted interface will be created in a new file.
    class ProtoA
    {
        public void MethodB(string s) { }
    }
    
  2. With the cursor positioned in MethodB, and click Extract Interface on the Refactor menu.

    The Extract Interface dialog box appears.

    You can also type the keyboard shortcut CTRL+R, I to display the Extract Interface dialog box.

    You can also right-click the mouse, point to Refactor, and then click Extract Interface to display the Extract Interface dialog box.

  3. Click Select All.

  4. Click OK.

    You see the new file, IProtoA.cs, and the following code:

    using System;
    namespace TopThreeRefactorings
    {
        interface IProtoA
        {
            void MethodB(string s);
        }
    }
    

This feature is only accessible when the cursor is positioned in the class, struct, or interface that contains the members that you would like to extract. When the cursor is in this position, invoke the Extract Interface refactoring operation.

When you invoke extract interface on a class or on a struct, the bases and interfaces list is modified to include the new interface name. When you invoke extract interface on an interface, the bases and interfaces list is not modified.

Show:
© 2014 Microsoft