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
[CanBeNull], you’ll appreciate that ReSharper now copies these attributes to the introduced fields or properties.
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.
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.
Make type parameter invariant
Sometimes a usage of a type parameter violates its variance. ReSharper offers a quick-fix to make the 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
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.
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:
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!
Subscribe to Blog updates
Thanks, we've got you!
Eager, Lazy and Explicit Loading with Entity Framework Core
Entity Framework Core (EF Core) supports a number of ways to load related data. There’s eager loading, lazy loading, and explicit loading. Each of these approaches have their own advantages and drawbacks. In this post, let’s have a quick look at each of these ways to load data for navigational prope…
OSS Power-Ups: bUnit – Webinar Recording
The recording of our webinar, OSS Power-Ups: bUnit, with Egil Hansen and Steven Giesel, is available. This was the twelfth episode of our OSS Power-Ups series, where we put a spotlight on open-source .NET projects. Subscribe to our community newsletter to receive notifications about future webi…
Accelerating Your Testing Workflow with Unit Test Creation and Navigation
Unit tests play an important role in our daily development workflow. They help us ensure our codebase's correctness when writing new functionality or performing refactorings to improve readability and maintainability. In the process, we often create new test files that accompany the p…
Introducing Predictive Debugging: A Game-Changing Look into the Future
With the introduction of debugging tools, software developers were empowered to interactively investigate the control flow of software programs to find bugs in live environments. At JetBrains, we've always strived to improve the art of debugging. Besides the more standard things you expect from a de…