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# – Duplicate ‘AssemblyVersion’ attribute

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 How to generate XML documentation and include it in a nuget package

C# – How to read a custom class from app.config with an automatic loader

It’s tedious to manually map settings from appSettings to your own class. You may have looked for ways to load your own custom class. The official documentation that explains how to do this is overly complicated. They would have you inheriting from ConfigurationSection, etc…, which is way too much effort. Why bother with all of … Read more C# – How to read a custom class from app.config with an automatic loader

C# – Merge two dictionaries in-place

When you merge two dictionaries, you can either merge them in-place, or create a new dictionary and copy the values over to it. The following extension method does an in-place merge of two dictionaries. It puts items from the right dictionary into the left dictionary. When duplicate keys exist, it’s keeping the value from the … Read more C# – Merge two dictionaries in-place

How to use toxiproxy to verify your code can handle timeouts and unavailable endpoints

When you have code that calls an endpoint, you need to make sure it’s resilient and can handle error scenarios, such as timeouts. One way to prove your code is resilient is by using toxiproxy to simulate bad behavior. Toxiproxy sits between your client code and the endpoint. It receives requests from your client, applies … Read more How to use toxiproxy to verify your code can handle timeouts and unavailable endpoints

C# – Auto-populate a dropdown with enum values

When you need to show enum values in a dropdown, it’s a good idea to automatically populate the list, instead of manually setting all of the values. To autopopulate the dropdown, set the DataSource to Enum.Values(), like this: Then to get the option the user picked, do the following: When I launch my form, I … Read more C# – Auto-populate a dropdown with enum values

System.BadImageFormatException: Could not load file or assembly

Problem – Can’t load assembly When you have one assembly trying to use another assembly, and they don’t have matching bitness (x64 or x86), then you’ll get an exception: either BadImageFormatException or FileLoadException. If your project references another assembly, and the bitness doesn’t match, you’ll get this exception: System.BadImageFormatException: ‘Could not load file or assembly … Read more System.BadImageFormatException: Could not load file or assembly

C# – Explicitly compare bool? with true/false to avoid compile-time errors and runtime exceptions

You can’t use nullable bools (bool?) exactly like regular bools, because they aren’t the same thing. When you try to use them like regular bools, you run into compiler errors and runtime exceptions. Fortunately, there is a simple, concise way to treat nullable bools almost-like regular bools. You have to explicitly compare them with true/false. … Read more C# – Explicitly compare bool? with true/false to avoid compile-time errors and runtime exceptions

Use string interpolation instead of string.Format to prevent runtime FormatExceptions

Using string.Format() is error prone and can lead to runtime exceptions. Using string interpolation prevents these problems (and it’s more readable). This article shows the two common mistakes you can make when using string.Format(), resulting in runtime exceptions, and shows how you can use string interpolation to prevent these problems. 1 – FormatException: Format string … Read more Use string interpolation instead of string.Format to prevent runtime FormatExceptions

.NET JsonSerializer is over 200x faster if you reuse JsonSerializerOptions

When you serialize an object using the built-in .NET JsonSerializer (System.Text.Json), reusing the JsonSerializerOptions object is 200x faster than serializing with a new options object each time. The JsonSerializerOptions object must have some kind of internal caching mechanism that makes subsequent serializations on the same type significantly faster. Note: Creating the new JsonSerializerOptions object adds … Read more .NET JsonSerializer is over 200x faster if you reuse JsonSerializerOptions