ReSharper 2024.3 Help

Convert Method to Indexer refactoring

This refactoring helps you convert methods that access collections into indexers and update method usages accordingly. You can apply it to:

  • A method with return value and one or more parameters to convert it into the get accessor of an indexer with the same set of parameters.

  • A method with void return type and two or more parameters (the last of which is expected to be a value and the others represent the index) to convert it into the set accessor of an indexer.

Consider the following example. If no indexer exists, the refactoring will create it:

class TestBookLibrary { Book[] _books; Book GetBookAt(int index) { return _books[index]; } void Insert(int index, Book book) { _books[index] = book; } void Copy(int copy, int to) { Insert(to, GetBookAt(copy)); } }
class TestBookLibrary { Book[] _books; Book this[int index] { get { return _books[index]; } } void Insert(int index, Book book) { _books[index] = book; } void Copy(int copy, int to) { Insert(to, this[copy]); } }

If an indexer with only one of the accessors exists, the refactoring will try to add the second accessor if the converted method fits:

class TestBookLibrary { Book[] _books; Book this[int index] { get { return _books[index]; } } void Insert(int index, Book book) { _books[index] = book; } void Copy(int copy, int to) { Insert(to, this[copy]); } }
class TestBookLibrary { Book[] _books; Book this[int index] { get { return _books[index]; } set { _books[index] = value; } } void Copy(int copy, int to) { this[to] = this[copy]; } }

Convert a method into an indexer

  1. Place the caret at the declaration or a usage of a method in the editor, or select it in the File Structure window window.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Convert Method to Indexer.

    • Right-click and choose Refactor | Convert Method to Indexer from the context menu.

    • Choose ReSharper | Refactor | Convert | Method to Indexer… from the main menu.

  3. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

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: 23 September 2024