Quick way to create a unit test project in Visual Studio

You don’t need to manually create a unit test project (through the UI or through the command line). Instead, you can can use the Create Unit Tests command in Visual Studio to generate a unit test project, add it to the solution, and wire up a test stub all at once. Note: This approach can … 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

System.Data.SqlClient is missing in a .NET Core project

When you create a new project in .NET Core, and you try to use a class from ADO.NET, such as SqlConnection, you’ll get missing reference errors. For example, your code might look something like this, and it’ll have red-squiggly lines under SqlConnection: In the past, you’d simply add a reference to System.Data.SqlClient, like this: To … Read more

.NET – Copy files to a specified directory after the build

The simplest way to copy files post-build in a .NET project is to use the MSBuild Copy Task in the .csproj file, like this: Note: I’m using VS2019. My project is called NotesAPI. When I build, it logs the following messages: It copied the following build files into C:\Build\NotesAPI: NotesAPI.dll NotesAPI.pdb In this article, I’ll … 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

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

How to generate XML documentation and include it in a nuget package

XML documentation comments serve two purposes: Intellisense shows the comments to devs using your code. You can generate a documentation file and include it in your build and nuget package. In this article I’ll show how to automatically generate an XML documentation file and how to include it in a nuget package. 1 – Write … 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