ReSharper 2017.2 Help

Convert Method to Indexer refactoring

ReSharper | Refactor | Convert | Method to Indexer…
ReSharper_Function2Indexer

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.

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

Consider the following example:

Before refactoringApplied to 'GetBookAt'Then applied to 'InsertBookAt'
class TestBookLibrary { private Book[] books; public Book GetBookAt(int index) { if (index >= books.Length) throw new IndexOutOfRangeException(); return books[index]; } public void InsertBookAt(int index, Book book) { if (index >= books.Length) throw new IndexOutOfRangeException(); books[index] = book; } public void TestCopyBook(int copy, int to) { InsertBookAt(to, GetBookAt(copy)); } }
class TestBookLibrary { private Book[] books; public Book this[int index] { get { if (index >= books.Length) throw new IndexOutOfRangeException(); return books[index]; } } public void InsertBookAt(int index, Book book) { if (index >= books.Length) throw new IndexOutOfRangeException(); books[index] = book; } public void TestCopyBook(int copy, int to) { InsertBookAt(to, this[copy]); } }
class TestBookLibrary { private Book[] books; public Book this[int index] { get { if (index >= books.Length) throw new IndexOutOfRangeException(); return books[index]; } set { if (index >= books.Length) throw new IndexOutOfRangeException(); books[index] = value; } } public void TestCopyBook(int copy, int to) { this[to] = this[copy]; } }

To convert a method to 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.
  2. Do one of the following:
    • Press Ctrl+Shift+R and then choose Convert Method to Indexer
    • Right-click and choose Refactor | Convert Method to Indexer in the context menu.
    • Choose ReSharper | Refactor | Convert | Method to Indexer… in 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 Feature is available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 14 December 2017

See Also