Tips and Tricks
Quick Tip: Use Quick Actions/Refactorings to Learn C#
With advanced IntelliSense/IntelliCode functionality continually being infused into Visual Studio 2019, some developers are using the Quick Actions and Refactorings light bulb to learn new C# coding tricks.
This was highlighted in a recent tweet by Dennis Delimarsky (@DennisCode):
"Literally learning new ways to write switch statements through automatic suggestions.
"You can do THIS in C# now?" Exploding head"
An inserted GIF shows how clicking on the light bulb and selecting "Convert switch statement to expression" can reduce the amount of code required, using the switch expression introduced in C# 8.0.
Several developers lauded the post and the idea in general, with comments such as:
- "Exactly half the lines of code to do the same thing, I loved it."
- "I've learned a few things like that when they were new, like inlining out parameters, and discards."
- "I have been writing code for 49 years and Visual Studio and C# is the best combination for writing code I have ever used."
- "And I just learnt a new switch syntax from Twitter."
So this reporter had to try it out and indeed duplicated exactly what Delimarsky did. It was super quick and easy.
Here's the procedure in an animated GIF:
To slow things down, here are screenshots showing the process:
Documentation for the switch expression -- dated April 2021 -- reveals that: "Beginning with C# 8.0, you use the switch
expression to evaluate a single expression from a list of candidate expressions based on a pattern match with an input expression. For information about the switch
statement that supports switch
-like semantics in a statement context, see the switch statement article."
Microsoft actually discussed this "Convert switch statement to switch expression" functionality back in August 2019 when C# 8.0 was still in preview, in an article titled "Visual Studio Tips and Tricks: Increasing your Productivity for .NET". "You can now convert a switch statement to a switch expression," it says. "In your project file make sure the language version is set to preview since switch expressions are a new C# 8.0 feature."
There's also a convert if statements to switch statements or switch expressions refactoring available for an easy transition between if
statements and switch
statements or expressions.
Other Refactorings introduced in various editions of Visual Studio 2019 include:
Of course, helping developers learn about new things while actually working in Visual Studio 2019 was greatly enhanced with the AI-driven update of IntelliSense, called IntelliCode, introduced in 2018. "As you type, AI-assisted IntelliSense recommends the most likely API. This makes it easier to learn a new API and dramatically reduces the number of keystrokes required to complete a line. With more context from the code you write, IntelliSense becomes more accurate," Microsoft said.
Returning to Delimarsky's tweet, several developers commented that similar functionality was also provided outside of Visual Studio proper:
- "Static analysis has about always been available in @EclipseJavaIDE with extensions such as Findbugs for instance."
- "Try SonarLint extension as well"
- "You'd probably also like the Visual Studio NuGet Style Cop. Bit only for code; It even has autocomplete for comments."
- "Had the same in ReSharper for years. Great for Linq as well."
- "Give a try to @JetBrainsRider, you may love it."
At the time of this writing, three days after Delimarsky's tweet was posted, it shows 21 comments, 106 retweets, 13 quote tweets and 595 likes.
About the Author
David Ramel is an editor and writer at Converge 360.