C# – Ignore null properties during JSON serialization

By default, null properties are included during JSON serialization like this: There are two ways to ignore null properties: Use a serialization setting that makes it ignore all null properties. Use an attribute to ignore a property if it’s null. In this article, I’ll show examples of these two ways to ignore null properties. I’ll … Read more

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# – Ignore the Nullable CS8618 warning in DTO classes

When you have the Nullable Reference Types feature (Nullable for short) enabled, one of the warnings you’ll run into is the following: CS8618 Non-nullable property X must contain a non-null value when exiting constructor. This warning doesn’t make sense in a very common scenario: You have a DTO class with a non-nullable reference type, such … 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

C# – Nullable Reference Types feature basics

The main purpose of the Nullable Reference Types (NRT) feature is to help prevent NullReferenceExceptions by showing you compiler warnings. You can make a reference type nullable (ex: Movie? movie) or non-nullable (ex: Movie movie). This allows you to indicate how you plan on using these references. The compiler uses this info while analyzing actual … Read more

C# – How to use JsonNode to read, write, and modify JSON

When you don’t want to create classes for JSON (de)serialization, one option is to use JsonNode. This allows you work with JSON as a mutable DOM that consists of JsonNode objects (JsonObject, JsonArray, JsonValue). You can use it to read, write, and modify JSON. Here’s an example. Let’s say you have the following JSON that … Read more

C# – Filter a dictionary

The simplest way to filter a dictionary is by using the Linq Where() + ToDictionary() methods. Here’s an example: Note: You can use the Dictionary constructor (new Dictionary<string, int>(filterList)) instead of ToDictionary() if you prefer. This produces a new dictionary with the filtered item: Where() produces a list (actually an IEnumerable) of KeyValuePair objects. Most … Read more

C# – Change a dictionary’s values in a foreach loop

In .NET 5 and above, you can directly change a dictionary’s values in a foreach loop. Here’s an example: This outputs the following: You couldn’t do this before .NET 5, because it would invalidate the enumerator and throw an exception: InvalidOperationException: Collection was modified; enumeration operation my not execute. Instead, you’d have to make the … Read more