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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events