Add/Remove params modifier refactoring
This refactoring allows you to improve APIs by introducing the params
modifier to the last parameter of a collection type.
note
Starting from C# 12, the params modifier can be used for all collection expressions. For earlier language versions, it can only be used with arrays.
Consider an example of an API that accepts the array of types as the last argument:
public sealed class AcceptsDataAttribute : Attribute
{
public AcceptsDataAttribute(string description, Type[] types) { }
}
Such API will force the consumers to construct arrays at the call site:
public class MyService
{
[AcceptsData("Flag/text", new[] { typeof(string), typeof(bool) })]
public void EntryPoint(object data)
{
// do something
}
}
With this refactoring, you can improve this API ergonomics by adding the params
modifier for Type[] types
parameter and updating all usages.
Before refactoring
public sealed class AcceptsDataAttribute : Attribute{ public AcceptsDataAttribute(string info, Type[] types) { }}public class MyService{ [AcceptsData("Flag/text", new[] { typeof(string), typeof(bool) })] public void EntryPoint(object data) { // do something }}
After refactoring
public sealed class AcceptsDataAttribute : Attribute{ public AcceptsDataAttribute(string info, params Type[] types) { }}public class MyService{ [AcceptsData("Flag/text", typeof(string), typeof(bool))] public void EntryPoint(object data) { // do something }}
Set the caret anywhere within the target method signature.
Do one of the following:
Press AltEnter and choose Add 'params' modifier and update usages.
Press CtrlShift0R and then choose Add 'params' modifier and update usages.
If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
tip
Right after the refactoring has been completed, all changes that it makes anywhere, including other files, are registered as a single operation. So you can use the Undo action Ctrl0Z to roll back all these changes with a single keystroke.
With this refactoring, you can also perform the reverse transformation by removing the params
modifier and updating the usages. This can be helpful ife you need to include new parameters after the last params
parameter.