How to set multiple startup projects in Visual Studio

Since VS2019, you can set multiple startup projects in the solution’s properties. This is useful when you have multiple projects in the same solution that you want to start at the same time (with or without debugging). Before this, you’d have to set a project as the startup project, start it, then repeat with all … Read more

C# – How to treat warnings like errors

Warnings are easy to ignore and forget about, which isn’t good. They point out potential problems that you might want to fix. To make it easier to pay attention to warnings, you can treat them like errors. You can choose which warnings to treat like errors by using settings in the project file. There are … Read more

Visual Studio – How to make the Implement Interface quick action generate auto properties

When you use the Implement Interface quick action in Visual Studio, it generates all members that throw exceptions, even the property getters and setters: This is fine for methods, but not for properties. You’d expect it to generate auto properties. Instead, it generates getters/setters that you then have to go clean up. Fortunately, this behavior … Read more

Visual Studio – How to use conditional breakpoints

Breakpoints cause execution to pause when you’re running the debugger. This is referred to as breaking, and it allows you to look at the current state of things for debugging purposes. In some cases, you may want to use a conditional breakpoint to only break execution when certain conditions are met (ex: break when name … Read more

Visual Studio project templates without minimal API and top-level statement

Devs want the option to create new projects in Visual Studio without all of the minimalist stuff that was recently added (top-level statement, minimal API). At the very least, these features should be optional instead of being the default without a way to opt-out (to be fair, minimal API is optional). What I typically do … Read more

Microsoft.TextTemplating.targets was not imported due to the file not existing

Problem You have a project that is using text templating and you’re upgrading to a new version of Visual Studio. When you open the project, you get error messages about not being able to import the Microsoft.TextTemplating.targets project: Project “…\v16.0\TextTemplating\Microsoft.TextTemplating.targets” was not imported by “…SomeProject.csproj” at (7,3), due to the file not existing. The error … Read more

How to add .gitignore in Visual Studio

It’s important to add .gitignore to your git repository. This tells git which files in your source directory to not track. Without this, you end up pushing lots of unnecessary files to your repository – such as build output files. The simplest way to add .gitignore is by using Visual Studio. This initializes it with … Read more

C# – IDE0060: Remove unused parameter

If you have a method with a parameter, and that parameter is not used in the method, then you’ll get the IDE0060 message telling you to remove the unused parameter. Here’s an example of code that would trigger this message: The encrypt parameter isn’t being used in the Send() method, triggering the IDE0060 message: IDE0060 … Read more

C# – Conditional compilation

You can exclude specific code from being compiled by using conditional compilation symbols. There are a few predefined symbols that support common scenarios – conditional compilation based on target framework (ex: .NET 5 vs .NET Core 3.1) and based on build configuration (Debug vs Release). In addition, you can add your own symbols to handle … Read more

How to enable the built-in .NET Analyzers

.NET Analyzers (which replaces FxCop) ships with the .NET 5 SDK. If you have the .NET 5 SDK installed, then you can enable .NET Analyzers by adding properties to the .csproj file. You can use this even if your project isn’t targeting .NET 5. If you don’t have the .NET 5 SDK installed, but want … Read more

C# – Duplicate ‘AssemblyVersion’ attribute

Problem You’re trying to add the AssemblyVersion attribute to your project, like this: And you get the following compiler errors: Error CS0579 Duplicate ‘AssemblyVersion’ attribute Error CS0579 Duplicate ‘AssemblyFileVersion’ attribute But you don’t see these attributes anywhere else in your project. Solution The problem is Visual Studio auto-generates the assembly info by default. To turn … Read more

C# – Use Assert.ThrowsException instead of ExpectedException attribute

If you’re using the built-in MSTest unit testing framework, you may be used to the old way of asserting exceptions by using the [ExpectedException] attribute. The following example illustrates one of the key problems with this approach: The ArgumentNullException is being thrown, so the test passes. However, the exception is being thrown in the arrange … Read more

Class Diagrams missing in Visual Studio

Problem The Class Diagram item is missing in Visual Studio. Note: I ran into problem starting in VS2019. Solution For some reason this is not installed by default in Visual Studio, so we simply need to install it. 1. In Visual Studio click Tools > Get Tools and Features… 2. Close Visual Studio. 3. In … Read more