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
Boost Code Quality with Qodana and GitHub Actions
It’s been roughly half a year since we introduced Qodana to .NET in our blog post about how to elevate your C# code quality with Qodana. Since then, we’ve been quite busy! Qodana went out of preview and into GA. Furthermore, we greatly improved the integration with our IDEs, providing an effortless …
Creating Custom AI Prompts
AI has swept through the software development industry like a wildfire. So people want to learn how to best use AI in their day to day tasks. In this post we’ll take a look at how to write custom prompts for use with the JetBrains AI Assistant in ReSharper and Rider so you can make the most of AI.&n…
12 Debugging Techniques In JetBrains Rider You Should Know About
Twelve must know debugging features in JetBrains Rider every developer should know.
Interceptors – Using C# 12 in Rider and ReSharper
Welcome to our series, where we take a closer look at the C# 12 language features and how ReSharper and Rider make it easy for you to adopt them in your codebase. If you haven’t yet, download the latest .NET 8 SDK and update your project files! In this series, we are looking at: Primary …