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

Convert Method to Indexer refactoring

Last modified: 08 March 2021

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: