Use this refactoring to convert:
- Get accessor of an indexer (default property in VB.NET) to a method with return value and a set of parameters that match those of the original indexer.
- Set accessor of an indexer, if any, to a void method with a set of indexer parameters and a value parameter.
The reverse functionality is provided by the
To convert an indexer (default property) to a method
- In the editor, place the caret at an indexer that you'd like to convert.
- On the ReSharper menu or context menu, choose Refactor | Convert | Indexer to Method. The Convert Indexer to Method dialog box displays:
- Use Convert getter and Convert setter check boxes to choose which accessors to convert, and specify target method names in Getter name and Setter name fields.
- Click Next to apply the refactoring.
Consider the following example:
| Before: |
public string this[short number]
{
get
{
string projectId;
if (myDictionary.TryGetValue(number, out projectId))
return projectId;
else
return null;
}
set
{
myDictionary.Add(number, value);
}
}
|
| After: |
public void SetItem(short number, string value)
{
myDictionary.Add(number, value);
}
public string GetItem(short number)
{
string projectId;
if (myDictionary.TryGetValue(number, out projectId))
return projectId;
else
return null;
}
|
