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

Using Visual Studio props files

When you want multiple projects in a solution to use the same project settings (some or all), you can put the settings in a shared props file. There are two options: I’ll show both options below. Note: You can also use a combination of these two options. Option 1 – Use Directory.Build.props You can use … 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 (or in … 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# – 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 loop through a dictionary and directly change its values. 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# – TimeZoneInfo with current UTC offset

TimeZoneInfo always shows the base UTC offset. This can be confusing because the UTC offset can change based on the date (due to daylight savings rules). Here’s an example showing DateTimeOffset and TimeZoneInfo with different offsets: You can get a date’s UTC offset from DateTimeOffset and combine it with TimeZoneInfo.DisplayName. This approach is implemented in … 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 the current date and time

Use DateTime.Now to get the current date/time, like this: This outputs the system’s current date/time: Note: By default, it uses the current culture’s date 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

The simplest way to loop through a dictionary is with a foreach loop. Here’s an example of initializing a dictionary with values and then looping through it: This outputs the following: The loop variable is a KeyValuePair<string, int> with Key and Value properties. Instead of using this, you can deconstruct the KeyValuePair into named variables, … 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

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

HackerRank – Zig Zag Sequence solution

In this article, I’ll explain the Zig Zag Sequence algorithm problem on HackerRank. Problem statement: You’re given an integer array with an odd number of elements (ex: [5, 2, 3, 1, 4]). You need to re-arrange the elements so they’re in a zig zag sequence, which means: Here’s a diagram to help you visualize what … Read more