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

Featured

  • Simulated Annealing Optimization Using C# or Python

    Dr. James McCaffrey of Microsoft Research shows how to implement simulated annealing for the Traveling Salesman Problem (find the best ordering of a set of discrete items).

  • New Lightweight JetBrains Editor Draws VS Code Comparisons

    Software development tooling vendor JetBrains debuted a new lightweight code editor that can act like a full-fledged IDE, immediately drawing comparisons to Microsoft's Visual Studio Code.

  • WinUI 3 Team Asked About Delayed Open Source Plans: 'What Happened?'

    A developer asked Microsoft's WinUI 3 dev team about delayed open source plans for the nearly two-year-old project that serves as the company's next-gen UI library for creating Windows apps.

  • Black White Wave IMage

    New Azure AI VMs Immediately Claim Top500 Supercomputer Rankings

    Visual Studio coders who dabble in artificial intelligence projects can now take advantage of new Azure virtual machines (VMs) featuring 80 GB NVIDIA GPUs that immediately claimed four spots on the TOP500 supercomputers list, Microsoft said.

Upcoming Events