ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Convert Method to Indexer refactoring

Last modified: 23 September 2024

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 more information about other languages, refer to corresponding topics in the Languages and frameworks section.