Convert to Tuple
- 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
Place the caret on a method declaration.
Note
The blinking cursor shows the caret’s position at which the Refactoring is available.
public class Library { public Library() { AddBook("The Hitchhiker's Guide to the Galaxy", "D. Adams"); AddBook("The Hobbit, or There and Back Again", "J.R.R. Tolkien"); } private List<Book> shelf = new List<Book>(); public│ void AddBook(string Title, string Author) { shelf = shelf ?? new List<Book>(); shelf.Add(new Book(Title, Author)); } }
- Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
- 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.
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));
}
}
See Also