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):

Visual Studio has become my C# teacher lately.
[Click on image for larger view.] "Visual Studio has become my C# teacher lately." (source: Twitter).
"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:

The Conversion in Animated Action
[Click on image for larger, animated GIF view.] The Conversion in Animated Action
To slow things down, here are screenshots showing the process:
The Original Switch Statement
[Click on image for larger view.] The Original Switch Statement
Visual Studio Previewing the Change
[Click on image for larger view.] Visual Studio Previewing the Change
The Resulting Switch Expression
[Click on image for larger view.] The Resulting Switch Expression

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.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube