C# – How to use format strings with string interpolation

Interpolated strings have the following structure: {variable:format}. Typically you exclude the format, so they normally look like this: $”My name is {name}”. Here’s how to use format strings with an interpolated string: This outputs the following: This is the equivalent of using string.Format() like this: As you can see, specifying a format string with an … Read more C# – How to use format strings with string interpolation

System.InvalidOperationException: Serializer options cannot be changed once serialization or deserialization has occurred

Problem When using System.Text.Json, it’s a good idea to reuse JsonSerializerOptions objects. This leads to a massive 200x speedup in subsequent calls to the serializer. The downside is you can’t change properties on the options object after you’ve passed it in a Serialize()/Deserialize() call. You’ll get the exception: System.InvalidOperationException: Serializer options cannot be changed once … Read more System.InvalidOperationException: Serializer options cannot be changed once serialization or deserialization has occurred

C# – Handle a faulted Task’s exception

When a Task throws an exception and stops running, it has faulted. The question is, how do you get the exception that was thrown from the faulted Task? This depends on if you’re awaiting the Task or not. This article shows how to handle a faulted Task’s exception in two scenarios: when you’re awaiting the … Read more C# – Handle a faulted Task’s exception

C# – Get a file’s checksum using any hashing algorithm

This article shows how to get a file’s checksum using any of these hashing algorithms: MD5, SHA1, SHA256, SHA384, and SHA512. If you are only interested getting a specific type of checksum, take a look at the first section. If you’re interested in a general-purpose checksum method that allows you to generate the checksum using … Read more C# – Get a file’s checksum using any hashing algorithm

File operations in .NET: Create, Read, Update, Delete, and Search

.NET has a good, high-level File API that abstracts away the complexity of file operations. You can create, read, update, delete, and search for files with a single method call. It’s flexible and provides multiple approaches, allowing you to pick the best tool for the job. For example, you can load an entire file all … Read more File operations in .NET: Create, Read, Update, Delete, and Search

C# – IPv4 range validation

Given a starting IPv4 address and an ending IPv4 address, how do you know if this is a valid range? The range is valid if: The starting IPv4 address is valid. The ending IPv4 address is valid. The range is a class C network with subnet mask 255.255.255.0. This means the starting IP and ending … Read more C# – IPv4 range validation

How to add User Secrets in a .NET Core console app

The User Secrets feature in .NET Core is a safe, simple way to override values in appsettings.json. The overridden values only exist in a file sitting in your own dev environment, so you don’t accidently commit them to your source control repository. This feature is enabled in ASP.NET by default, and the framework does most … Read more How to add User Secrets in a .NET Core console app

ASP.NET – How to add User Secrets

To add User Secrets in an ASP.NET project in Visual Studio do the following steps: Right-click the project and click Manage User Secrets. This will create and open a file called secrets.json. Override appsettings.json settings by putting them in secrets.json For example, let’s say your appsettings.json looks like this: You want to override ApiKey. In … Read more ASP.NET – How to add User Secrets

Set Operations – Intersect, Union, Difference, and Symmetric Difference

In the show House, different sets of main characters appeared in different seasons. For example, here are the main characters that starred in seasons 1 and 7: Characters in Season 1 Characters in Season 7 HouseCuddyWilsonForemanChaseCameron HouseCuddyWilsonForemanChaseTaubThirteenMasters Set operations allow you to answer interesting questions, such as “Which House main characters appeared in both seasons … Read more Set Operations – Intersect, Union, Difference, and Symmetric Difference