How-To's

A summary of ReSharper actions around C# method parameters

ReSharper offers hundreds of quick-fixes and context actions. As you remember, quick-fixes are aimed to resolve a certain problem in your code, whereas context actions allow you to quickly introduce some minor changes.

Today we are going to review several quick-fixes and context actions that simplify your work with method parameters in C#. Some of them have been available for years, some have only been added in the most recent release.

Introduce fields and auto-properties for all unused parameters

Since v1.0 ReSharper has had a very useful quick-fix to Introduce read-only field for unused constructor parameter. As C# has evolved, we’ve been improving ReSharper to support new language features. Create and initialize auto-property quick-fix appeared as part of C# 3.0 support. Back in 2015, C# 6.0 brought us get-only auto-properties and we were asked to add the ability to define get-only auto-property and initialize it from a parameter.

Progress never stops, so for ReSharper 2016.3 we came up with a few more enhancements to these quick-fixes. Now you can not only select the kind of property to introduce, but also configure the default behavior. And since it’s tedious to invoke Alt+Enter menu for each parameter, we’ve also extended Introduce read-only field and Introduce auto-property quick-fixes with an option to apply them to all parameters at once. But there’s more to it! If you use ReSharper’s JetBrains.Annotations to mark constructor parameters as [NotNull] or [CanBeNull], you’ll appreciate that ReSharper now copies these attributes to the introduced fields or properties.
Introduce field or auto-property for all unused parameteres

Check all parameters for null

This context action came before the previous quick-fix and actually led to its implementation. Check all parameters for null is very useful when your method has multiple arguments of different types which could potentially have null value. There’s also an option to check for an empty or whitespace string.
Checking parameters for null

Introduce optional parameters

ReSharper helps you simplify the code in situations when the only purpose of an overloaded function is to call the implementation function with default values for some arguments. The Introduce optional parameters quick-fix removes the overload to use optional parameters in the ‘implementation’ function.
Optional parameters

Make type parameter invariant

Sometimes a usage of a type parameter violates its variance. ReSharper offers a quick-fix to make the parameter invariant.
Making parameter invariant

Migrate to IEnumerable in method parameters and returns

ReSharper scans your code base to detect methods that can safely return and accept IEnumerable instead of a more specific type such as Array, List, or ArrayList.
Migrating to IEnumerable

Remove redundant array creation

If a C# method takes a variable number of arguments with the params keyword, an array for the arguments will be generated by the compiler, so you don’t have to create an array for your arguments in the method call. What’s more interesting, ReSharper suggests a quick-fix that removes all redundant code in the desired scope, including redundant qualifiers, arguments, casts, condition checks, unused assignments, unreachable code, and more.
Removing redundant array creation

Rearrange code elements

Last but not least, we’d like to remind you of ReSharper’s code rearrangement feature, which is neither a quick-fix nor a context action. It can be applied to statements, expressions, type members, and other elements in the code. Let’s see how quickly you can change the order of arguments in the method by pressing Ctrl+Shift+Alt+Right and Ctrl+Shift+Alt+Left:
Rearranging method parameteres

To move the code element up or down, use the corresponding arrow key in combination with Ctrl+Shift+Alt.

Now it’s your turn

ReSharper has more context actions and quick-fixes for method parameters that can be real time-savers for you. We’d love to hear about your experience with ReSharper. Which of the actions we’ve just described have you been using regularly? Did we fail to mention your favorite coding assistance feature? Let us know!

image description