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 for Converge360.

comments powered by Disqus


  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube