Home > dotnet > Inline Diagnostics in Visual Studio 2022 for C# Code

Inline Diagnostics in Visual Studio 2022 for C# Code

Making C# warnings and errors more visible in the visual studio editing surface

by

Over the weekend I discovered a new feature in Visual Studio 2022 for C# code. This feature, which I’m referring to as “Inline Diagnostics” allows you to see compiler warnings and errors in your editor window without needing to hover over the tooltips.

A sample inline diagnostics warning is displayed below:

This is an always-present indicator of a potential flaw in your code. This means that compiler warnings and errors are no longer stuck in your Error List window but instead are visible as you browse your code.

This content is also available in video form on YouTube.

Setting up Inline Diagnostics for C# Code

To enable these experimental features, you need to be on Visual Studio 2022 or later. I’ve noticed this feature on version 17.3.3 but I imagine it arrived significantly before that version.

To enable inline diagnostics go to the Tools menu in Visual Studio and then select Options… at the bottom.

Next, expand Text Editor and find C# inside of it. Expand C# and select the Advanced node within C#.

Once the Advanced tab inside of Text Editor / C# is selected, find the “Display diagnostics inline (experimental)” check box and check it.

You also have the option to choose to align warnings / errors on the right edge of the editor window if you’d like, but I’ve found this a little harder to read.

Click OK and you should see these changes take place as your code is re-analyzed.

My Opinion on Inline Diagnostics

So what do I think of this feature?

My initial thought is that the user interface for this is a little aggressive. However, on further reflection I think that’s part of the point.

The Visual Studio team wants to make sure you know when warnings and errors are present.

I can think of a few key advantages of turning on inline diagnostics:

  • It makes it harder for potential problems to hide in the Errors List and Output (including security and memory issues)
  • It puts relevant warnings alongside the code as you’re scanning the code
  • If you’re using a laptop trackpad, touch screen, or have mobility issues, hovering over the the variable to see the traditional warning message is going to be difficult
  • New learners don’t understand that faint green or yellow lines indicate a warning or suggestion so they don’t know to hover over for more information

This last reason is the most compelling for me.

Inline Diagnostics for New C# Learners

I teach .NET development at a bootcamp full time as my primary job. Most new developers don’t understand the subtle ways Visual Studio is trying to indicate problems with their code.

It is very normal for me to walk over to assist a student and see very quickly that they have an unused variable they meant to use. The IDE hints it to them with grey text and green squigglies, but they don’t notice.

Having a blatant warning box like this would be significantly more aggressive and make it clear to the learner what is wrong with their code.

I do have some fears that it might be too aggressive, and I’d almost rather this run on a long time-delay or wait until build, but I really like this feature overall for the benefit of my “baby developers”.

I’d love to hear your own thoughts on this inline diagnostics for C# code feature – or other related features I might not be aware of yet.

Author

  • Matt Eland

    After several decades as a software engineer and engineering manager, Matt now serves as a software engineering instructor at Tech Elevator where he gets to raise up future developers and unleash them upon the world to build awesome things. Matt is an Azure Data Scientist and AI Engineer Associate, runs a data science blog and YouTube channel, is currently pursuing a master's degree in data analytics, and helps organize the Central Ohio .NET Developer Group. In his copious amounts of spare time, Matt continues to build nerdy things and looks for ways to share them with the community.

Leave a Reply

Related Content

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More