ReSharper
 
Get ReSharper
You are viewing the documentation for an earlier version of ReSharper.

Convert Method to Indexer refactoring

Last modified: 21 July 2022

ReSharper | Refactor | Convert | Method to Indexer…

To assign a shortcut, go to Tools | Options | Environment | Keyboard and find the ReSharper_Function2Indexer command.

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:

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

This feature is supported in the following languages and technologies:

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.