Bagikan melalui


How to: Refactor Code with Extract Interface

Use this procedure to perform the Extract Interface refactoring operation.

To use Extract Interface

  1. Create a console application as described in the Example section.

    For more information, see Creating Console Applications (Visual C#).

  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);
        }
    }
    

Example

To set up this example, create a console application named ExtractInterface, and then replace Program with the following code. For more information, see Creating Console Applications (Visual C#).

    // Invoke Extract Interface on ProtoA.
    // Note:  the extracted interface will be created in a new file.
    class ProtoA
    {
        public void MethodB(string s) { }
    }

See Also

Concepts

Refactoring

Reference

Extract Interface

Extract Interface Dialog Box