ASP.NET Core – Logging requests and responses

The simplest way to log requests/responses is to use the HTTP Logging middleware (added in v6). This is configurable, so you can make it suit your needs. If you need more control, you can add your own middleware instead. To use the HTTP Logging middleware, call UseHttpLogging() in your initialization code: Then add Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware to … Read more

C# – Get rid of Nullable warnings when you’re checking for null in another method

When you’re calling a helper method that does null checking (throw-if-null), you’ll still get Nullable warnings in the calling code because the compiler doesn’t know you’re already doing null checking. Here’s an example: ThrowIfNull() throws an exception if employee is null, so you know for sure that it won’t be null in the rest of … Read more

Hello World with React

I’m going to show you how to create a Hello World web app with React. Note: I’m using React 18, which is the latest version at the time of this writing. 1 – Install VS Code VS Code is a really good IDE for developing React apps. So first things first, go download and install … Read more

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: In this article, I’ll show examples of these two ways to ignore null properties. I’ll show how to do it with System.Text.Json and Newtonsoft. Ignore null properties with System.Text.Json Use JsonIgnoreCondition.WhenWritingNull to ignore null properties. You … 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 can just ignore the warning when it applies to DTOs. … 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

C# – How to use TimeZoneInfo

Time zones are complicated and their rules can change, so it makes sense to use a library when you’re dealing with them. One option in .NET is to use the built-in TimeZoneInfo class. Here’s an example of using TimeZoneInfo to get the local system’s time zone: This outputs: Note: The display name always show the … Read more

C# – Get key with the max value in a dictionary

The simplest way to get the key with the max value in a dictionary is to use the Linq MaxBy() method (added in .NET 6). This returns the key/value pair with the max value. Here’s an example: This outputs the following: In this article, I’ll show examples of getting the max by key, min by … Read more

C# – Get the current date and time

Here’s an example of how to get the current date/time: This outputs the current local date/time: Note: By default, it uses the current culture’s format (from the OS). This is showing the US date format – MM/dd/yyyy. DateTime.Now is the local date/time from the system where the code is executing. Keep that in mind if … Read more

C# – Loop through a dictionary

When you loop through a dictionary, whether it’s with a foreach or with a Linq method, you’ll be dealing with KeyValuePair objects, which have Key and Value properties (same types as the dictionary). Here’s an example of how to loop through a dictionary with a foreach: Note: The loop variable (kvp) is a KeyValuePair<string, int> … Read more

WinForms – Loop through a form’s controls

Forms also have a collection of controls (Controls property) that you can loop through. This is useful for when you want to do something to multiple controls and don’t want to have to manually type out code to deal with individual controls. Here’s an example of looping through a form’s top-level controls: Note: In the … Read more

WinForms – ComboBox with enum description

By default, when you load enum values into a ComboBox, it’ll show the enum names. If you want to show the enum descriptions (from the [Description] attribute) instead, and still be able to get the selected enum value, you can do the following: Generate a list of objects containing the enum value/description pairs. Set ComboBox.DataSource … Read more

C# – Check if a property is an enum with reflection

When you’re using reflection to look at a type’s properties, you can use PropertyInfo.PropertyType.IsEnum to check if the property is an enum. This is helpful when you want to be able to safely call an Enum API method (such as Enum.Parse()) on the reflected type, thus preventing an exception – ArgumentException: Type provided must be … Read more

CA2208: Instantiate argument exceptions correctly

The CA2208 code analysis rule checks for common mistakes when constructing argument exceptions. There are three main argument exception classes: ArgumentException, ArgumentNullException, and ArgumentOutOfRangeException. Unfortunately, it’s easy to make a mistake when using these. I’ll explain the common mistakes that CA2208 checks for and how to fix them (and when to suppress the warning instead). … Read more