System.InvalidOperationException: Collection was modified; enumeration operation may not execute

If you try to add/remove items from a collection while it’s being looped over in a foreach loop (enumerated), then you’ll get the following exception: System.InvalidOperationException: Collection was modified; enumeration operation may not System.Collections.Generic.List`1.Enumerator.MoveNext() This error can happen in two scenarios: You’re looping over the collection in a foreach loop and modifying it (add/removing) … Read more

Case sensitivity in SQL Server

In SQL Server, the collation property controls case sensitivity. Case sensitivity affects sorting and queries (even the column names must match exactly if you’re using a case-sensitive collation at the database-level). The default collation when you create a database in SQL Server is SQL_Latin1_General_CP1_CI_AS. The CI stands for case-insensitive, which means SQL Server databases are … 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

ASP.NET Core – How to unit test an ApiController

The key to unit testing an ApiController class is to mock out all of its dependencies, including the controller’s HttpContext property, like this: If the controller method you’re testing uses anything from the HttpContext, then you’ll want to swap in your own value. Otherwise HttpContext will be null and you’ll get a NullReferenceException. Fortunately Microsoft … 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# – Try/finally with no catch block

Try/finally blocks are useful for when you are required to do something at the end of a method no matter what. The finally block always executes, even if there’s an exception (there is one case where this isn’t true, which I’ll explain in the Unhandled exception section below). There are a few common scenarios where … Read more

CA1806: When a method returns a new instance and the instance is ignored

The official title of the CA1806 analyzer warning is “Do not ignore method results,” but this doesn’t really show up in the list of warnings. Instead of showing this generic message, when CA1806 is triggered, it shows very specific text about what triggered the warning. In this article, I’ll show a few examples of code … Read more

NLog – Log to console

There are two configuration options for logging to the console using NLog: Console target – uses the default text colors. ColoredConsole target – allows you to configure the color of logging messages. In this article, I’ll show how to configure these two targets using nlog.config. At the end, I’ll show an example of configuring NLog … Read more