ReSharper Web Help

This refactoring serves the opposite purpose to Convert Indexer (Default Property) to Method . That is, it converts:

  • Method with return value and a set of parameters to get accessor of an indexer with the same set of parameters.
  • Void method with a set of indexer parameters and a value parameter to set accessor of an indexer.
Method calls will be converted accordingly to indexer usages.

To convert a method to an indexer (default property)
  1. In the editor, place the caret at a method that you'd like to convert.
  2. On the ReSharper menu or context menu, choose Refactor | Convert | Method to Indexer.
    • If conflicts are found that might prevent ReSharper from successfully performing the refactoring, a dialog box shows up with links to code lines that originated the conflict:

      Use the links to find conflicting code and modify it in the text editor without closing the dialog box. After you've made necessary amendments, click Refresh. Once there are no conflict entries left in the dialog box, click Next to complete the refactoring.
    • If no conflicts are found, the selected method is converted in dialogless mode. Otherwise, resolve conflicts .

Consider the following example:

Before:
public void SetItem(string projectId, short value)
    {
    myDictionary.Add(projectId, value);
    }

public short GetItem(string projectId)
    {
    short number;
    if (myDictionary.TryGetValue(projectId, out number))
        return number;
    else
        return -1;
    }
After:
public short this[string projectId]
    {
    set { myDictionary.Add(projectId, value);
    }

    get
    {
        short number;
        if (myDictionary.TryGetValue(projectId, out number))
            return number;
        else
            return -1;
        }
    }

See Also

Procedures