C# – Check if key exists in dictionary

When you want to check if a key exists, use Dictionary.ContainsKey(). This returns true if the key exists and otherwise returns false. Here’s an example: This initializes the dictionary with a few items and then checks if one of the key exists. This outputs: Usually you’ll want to do something based on if the key … Read more

C# – Update value in a dictionary

After you add a key/value pair to a dictionary, you can update the value for the key by using the indexer syntax (i.e. dictionary[key]=value), like this: This outputs the updated value for the ‘Bob’ key: The indexer syntax inserts if the key doesn’t exist -or- updates the value if the key already exists. This means … Read more

C# – Add to a dictionary

The simplest way to add a key/value pair to a dictionary is by using Dictionary.Add(), like this: If the key already exists, Dictionary.Add() throws an ArgumentException because the key must be unique. There are a few other ways to add to a dictionary in different scenarios, which I’ll explain below. Add or update key/value in … Read more

C# – Validate an IP address

Use IPAddress.Parse() to parse an IP address from a string. This handles both IPv4 and IPv6 addresses and throws an exception if the string can’t be parsed into a valid IP address. Here’s an example: This outputs the following: Use IPAddress.TryParse() if you don’t want exceptions to be thrown. It returns false if the string … Read more

C# – Convert a string to an int

There are three ways to convert a string to an int: I’ll show examples of using these approaches. Use int.Parse() int.Parse() takes a string and converts it to a 32-bit integer. Here’s an example of using int.Parse(): When conversion fails int.Parse() throws an exception if it can’t convert the string to an int. There are … Read more

C# – How to convert char to int

Converting a char to an int means getting the numeric value that the char represents (i.e. ‘1’ to 1). This is not the same as casting the char to an int, which gives you the char’s underlying value (i.e. ‘1’ is 49). There are three ways to convert a char to an int: I’ll show … Read more

OverflowException: Value was either too large or too small for an int32

Problem When you try to convert a string to an integer with int.Parse() (or Convert.ToInt32()), you get the following exception: System.OverflowException: Value was either too large or too small for an Int32. The problem is the integer value in the string can’t fit in the 32-bit integer. Int32 (int) can only hold values between -2147483648 … 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

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

C# – Using top-level statements

The top-level statements feature makes the Main() method implicit. This feature was added in C# 9 (.NET 5) with the purpose of decluttering a project’s entry point. Here’s what a console app looks like using a top-level statement: The compiler generates the Main() method implicitly from the top-level statements. The code above is functionally equivalent … Read more

C# – Map query results to multiple objects with Dapper

When you’re querying joined tables, you can map each row to multiple objects by using the multi mapping feature in Dapper. To multi map, you have to provide Dapper with the following: In this article, I’ll show examples of multi mapping. Note: If you don’t specify the split column, it’ll use the default of “Id”. … Read more

ASP.NET Core – Getting query string values

The ASP.NET Core framework automatically parses query strings (i.e. ?name=Dune&year=2021) into HttpContext.Request.Query and maps the query string values to parameters in the action method (if you’ve added them). You can get the mapped query string values by adding action parameters, like this: Or you can use HttpContext.Request.Query directly (which is useful in many scenarios): This … Read more

C# – Check if a directory is empty

The simplest way to check if a directory is empty is by calling Directory.EnumerateFileSystemEntries(). If this doesn’t return anything, then the directory doesn’t have any files or subdirectories (folders), which means it’s empty. Here’s an example: Another situation to consider is when a directory has no files but might have empty subdirectories (folders). You can … Read more