ReSharper Help

Transform Out Parameters

ReSharper | Refactor | Convert | Transform Out Parameters…
ReSharper_TransformOutParameters

This refactoring lets you get rid of out parameters of a method. If the method's return type is void and a single out parameter is transformed, ReSharper moves the out value to the method's return. Otherwise, ReSharper creates a Tuple object to combine all return values. In any case, all method calls are updated accordingly.

The example below illustrates the first case, when the out parameter is replaced with method's return:

Before refactoringAfter refactoring
void Foo(int arg1, out string result) { result = "result"; // Try do something } private void Test() { string result; Foo(5, out result); Console.WriteLine(result); }
string Foo(int arg1) { var result = "result"; // Try do something return result; } private void Test() { string result; result = Foo(5); Console.WriteLine(result); }

If the method needs to return more than one value, a Tuple object is created:

Before refactoringAfter refactoring
bool TryFoo(int arg1, out string result) { result = "result"; // Try do something return true; } private void Test() { string result; if (TryFoo(0, out result)) Console.WriteLine(result); }
Tuple<bool, string> TryFoo(int arg1) { var result = "result"; // Try do something return Tuple.Create(true, result); } private void Test() { var tuple = TryFoo(0); var result = tuple.Item2; if (tuple.Item1) Console.WriteLine(result); }

To transform out parameters

  1. Place the caret at the declaration or a usage of method in the editor, or select the method in the File Structure window. Or, alternatively set the caret at the out keyword or at the name of an out parameter.
  2. Do one of the following:
    • Press Ctrl+Shift+R and then choose Transform Out Parameters
    • Right-click and choose Refactor | Transform Out Parameters on the context menu.
    • Choose ReSharper | Refactor | Convert | Transform Out Parameters… in the main menu.
    The Transform Out Parameters dialog will open.
  3. Select the parameters you want to transform. According to the selected parameters, a new signature is displayed in the bottom of the refactoring dialog.
  4. To apply the refactoring, click Next.
  5. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
Transforming method's out parameters

This feature is supported in the following languages/technologies:

C# VB.NET C++ HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts
feature_available feature_available

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.

See Also

Last modified: 27 August 2015