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!

Comments below can no longer be edited.

9 Responses to A summary of ReSharper actions around C# method parameters

  1. Avatar

    Hazzik says:

    November 7, 2013

    Please release previous versions of SDK

  2. Avatar

    Bill Sorensen says:

    November 8, 2013

    Any chance of adding prebuilt versions of some of the samples to NuGet? In particular, we have to rebuild SamplesCyclomaticComplexity with every major version of ReSharper.

  3. Avatar

    Matt Ellis says:

    November 11, 2013

    Yep. Some of the more useful samples are already on the gallery – e.g. the cyclomatic complexity extension is here:

    They’re currently available for 8.0, I’ll be updating them for 8.1 this week.

  4. Avatar

    Matt Ellis says:

    November 11, 2013

    @Hazzik – we’ve currently got 8.0 and the 8.1 EAP SDKs released, and we’re looking into releasing the 7.1 SDK too.

  5. Avatar

    Daniel Marbach says:

    November 12, 2013

    The 8.0 SDK seems to require target framework 4.0. Is that right? This is a breaking change. The previous versions allowed to have target version 3.5

  6. Avatar

    Mike-EEE says:

    February 6, 2017

    Absolutely LOVEEEEEE (it’s February, right?) the new “for all in project” feature. Use it everywhere. R# feels really solid right now. Well, outside dotCover, of course. That part is still quirky/buggy and could use a full reboot from scratch methinks. I still have yet to have seen a unit testing UI that I feel accurately captures the domain problem. For starters I think results should be in an extremely responsive, powerful, and filtered grid.

    In any case, really enjoying 2016.3 and I really have no inclination to upgrade to VS2017 any time soon, especially with all the fires and issues that are being reported around it. Seems like they should rename it to VS2018. 😛

    • Avatar

      Svetlana Emelyanova says:

      February 6, 2017

      Mike, thanks for your feedback!

  7. Avatar

    David French says:

    June 16, 2017

    One thing I would really like is, when I’m going to use a function (especially one that has several parameters), I would like to be able to auto-generate variables for each of the function’s parameters right before where I call the function. Auto-generate the variable names and types based on the function parameter names and types.

    • Avatar

      David French says:

      June 21, 2017

      …And automatically invoke the function with those auto-generated variables entered into the function arguments. Also, maybe make a preference for whether to use named parameters in the function call.

Discover more