This How-To explains how to take advantage of ReSharper’s features to quickly convert your arrays into generic collections.
In .NET 1.1, if you wanted to have some typed interface to return some items, your only choice was to use arrays, since collections were untyped:
If the implementation of INode did not keep an array of subnodes, one had to allocate a fresh array on every
In .NET 2.0, however, one can use generic collections. We would aim for our interface to look like this:
Generic collections a very good thing for a whole number of reasons that we won’t get into here.
So, now that people are migrating from 1.1 to 2.0, it would be great to somehow convert legacy interfaces from arrays to collections. For this, ReSharper comes in extremely handy. We ourselves used the following procedure when developing ReSharper 2.0 and we think it can be useful to you, too.
- Invoke the Change Signature refactoring (keyboard shortcut Ctrl + F6) on
INode.GetSubNodes()to change the return type from INode to IList<INode>:
The return type of all implementations is changed accordingly.
- After you compile, the code will, of course, contain errors such as this one:
You can identify these compilation errors and navigate to them by using the error stripes on the right-hand sidebar:
TIP: You can also navigate between compilation errors in the file by using keyboard only. Press F12 to go the next error and Shift+F12 to go to the previous error.
So, for errors like the one shown above, ReSharper suggests a quick-fix (keyboard shortcut Alt + Enter) to help you change the type of ‘subNodes‘ to
- However, another kind of error remains: the ‘Length’ member should be changed to ‘Count’ everywhere:
Just use the Change All quick-fix to quickly change all ‘Length‘s into ‘Count‘s:
ReSharper will suggest a list of members and, in this case, will automatically guess that
Countshould be the right member.
This concludes our conversion.
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…