ReSharper 2024.3 Help

Convert Abstract Class to Interface refactoring

This refactoring allows you to convert an abstract class to an interface. This can be useful when you want a class to inherit from more than one abstract classes — then you can easily convert one of them to an interface.

Consider the example below. After applying the refactoring, a class is converted into an interface:

public abstract class CachedItem { public abstract int Id { get; set; } public abstract void Register(); public abstract void UnRegister(); }
public interface CachedItem { int Id { get; set; } void Register(); void UnRegister(); }

Convert an abstract class into an interface

  1. Select an abstract class in one of the following ways:

    • In the editor, place the caret at the name of an abstract class.

    • Select an abstract class in the Solution Explorer.

    • Select an abstract class in the File Structure window window.

    • Select an abstract class in the Class View.

    • Select an abstract class in the Object Browser.

    • Select an abstract class in the type dependency diagram.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Convert Abstract Class to Interface.

    • Right-click and choose Refactor | Convert Abstract Class to Interface from the context menu.

    • Choose ReSharper | Refactor | Convert | Abstract Class to Interface… from the main menu.

  3. If the abstract class contains non-abstract members, ReSharper shows you a dialog notifying that the bodies and/or accessors of such members will be removed. To avoid this, you need to move the logic of non-abstract members to child classes or elsewhere manually, and then invoke the refactoring again.

  4. If no conflicts are identified, the abstract class is converted into an interface with the same name, and modifiers of abstract members are changed accordingly. Otherwise, resolve conflicts.

By default, the naming style for interfaces requires interface names to start with I. Therefore, if you did not change the settings, the interface name will be highlighted as inconsistently named. Just press Alt+Enter and use the corresponding quick-fix to  rename the interface.

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is not available in JavaScript

Feature is not available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the Languages and frameworks section.

Last modified: 25 September 2024