I write about coding problems (mostly C#) and mentor other devs
Whenever you’re dealing with code that can run into transient errors, it’s a good idea to implement retries. Transient errors, by definition, are temporary and subsequent attempts should succeed.
You can use dictionaries with tuples as the keys or values. I’ll show examples below. Tuples have two or more fields, so using a tuple as the key is a simple way to have compound keys in a dictionary.
The simplest way to make a copy of a dictionary is to use the copy constructor. You pass the dictionary you want to copy to the new dictionary’s constructor, like this: Note: If you’re using a non…
The simplest way to convert a dictionary to a list is by using the Linq ToList() method, like this: When used on a dictionary, ToList() returns a list of KeyValuePair objects. This example outputs...
I’ll show examples of how to deserialize JSON to dictionaries in different scenarios. When your JSON object has properties with simple string values (as opposed to nested objects)…
When you want to convert a dictionary to a JSON string, you can use the built-in JsonSerializer (from System.Text.Json) to serialize the dictionary. Here’s an example: Note: This is passing in…
Normally you’d check if a dictionary contains a key or get a value by key. But you can also check if the dictionary contains a specific value by using Dictionary.ContainsValue(). Here’s an example…
Dictionaries contain key/value pairs. The keys must be unique, but the values can be repeated many times. When you want to find duplicate values, the simplest option is to use Linq methods GroupBy()…
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’…
Dictionaries have keys mapped to values, which enables you to efficiently lookup values by key. But you can also do a reverse lookup: get the key associated with a value. The simplest option is to...
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…
Dictionaries contain key/value pairs. When you want to remove one or more items from a dictionary, you can use one of the following methods: I’ll show examples below. Use Dictionary.Remove() to...
Dictionaries store key/value pairs. When you want to get the value for a key, you can use the indexer syntax and specify the key, like this: Note: In order to show how to get a value…
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...
You can use Path.GetTempPath() to get the user’s temp folder path and then create a temp file in it. Alternatively, use the shortcut Path.GetTempFileName().
Shows how to use the IPAddress class to parse and validate an IP Address string. Also shows how to specifically validate an IPv4 address in the 3-dot format
Examples of getting files from a folder with Directory.GetFiles() and Directory.EnumerateFiles(), including from subfolders. Also shows how to get file names.