Back to Devexpress

Convert to Tuple

coderushforroslyn-115476-refactoring-assistance-convert-to-tuple.md

latest2.4 KB
Original Source

Convert to Tuple

  • Aug 03, 2020
  • 2 minutes to read

Purpose

Consolidates selected parameters into a Tuple object.

Note

Convert to Tuple is a cascading Refactoring. That means that the Refactoring affects all method calls and method declarations in interfaces, base and descendant classes. For instance, if you apply this Refactoring to the method declaration within an interface, it also changes the appropriate method declarations in all implementors and all calls to these methods.

Availability

Available in the following cases.

  • When the caret is on a method declaration or call, provided that it has more than one parameter.
  • When two or more parameters are selected in a method declaration or call.

Usage

  1. Place the caret on a method declaration.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Convert to Tuple from the menu.

After execution, the Refactoring converts the parameters into a Tuple object and replaces all usages according to the change.

csharp
public class Library {
    public Library() {
        AddBook(new Tuple<string, string>("The Hitchhiker's Guide to the Galaxy", "D. Adams"));
        AddBook(new Tuple<string, string>("The Hobbit, or There and Back Again", "J.R.R. Tolkien"));
    }
    private List<Book> shelf = new List<Book>();
    public void AddBook(Tuple<string, string> tuple) {
        shelf = shelf ?? new List<Book>();
        shelf.Add(new Book(tuple.Item1, tuple.Item2));
    }
}
vb
Public Class Library
    Public Sub New()
        AddBook(New Tuple(Of String, String)("The Hitchhiker's Guide to the Galaxy", "D. Adams"))
        AddBook(New Tuple(Of String, String)("The Hobbit, or There and Back Again", "J.R.R. Tolkien"))
    End Sub
    Private shelf As New List(Of Book)()
    Public Sub AddBook(ByVal lTuple As Tuple(Of String, String))
        shelf = If(shelf, New List(Of Book)())
        shelf.Add(New Book(lTuple.Item1, lTuple.Item2))
    End Sub
End Class

See Also

Remove Unused Parameter

Promote to Parameter