ReSharper 2024.2 Help

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.

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.

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 } }
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 } }

Add 'params' modifier

  1. Set the caret anywhere within the target method signature.

  2. Do one of the following:

    • Press Alt+Enter and choose Add 'params' modifier and update usages.

    • Press Control+Shift+R and then choose Add 'params' modifier and update usages.

  3. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

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.

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is not available in JavaScript

Feature is not available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the ReSharper by language section.

Last modified: 30 July 2024