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

System.ArgumentException: An item with the same key has already been added

Problem Dictionaries require keys to be unique. When you try to add a key/value to a dictionary and the key already exists, you get the following exception: System.ArgumentException: An item with the same key has already been added. This can happen when you use Dictionary.Add() or when you initialize a dictionary using the “curly brace” … Read more

C# – Get value from dictionary

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, I had to initialize the dictionary with key/value pairs. This returns the value associated with the “Bob” key and … 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# – How to use JsonExtensionData

Use the JsonExtensionData attribute (in System.Text.Json) to deserialize properties that aren’t part of the class. Without this, JSON fields that don’t match a property are simply ignored and the returned object will have null properties. Here’s an example of adding this attribute to a dictionary property: Now when you deserialize JSON to this Person class, … Read more

C# – Add or overwrite a value in ConcurrentDictionary

The simplest way to add or overwrite a value in a ConcurrentDictionary is to use the indexer: If the key doesn’t exist, this adds it. If the key exists, this overwrites it. The indexer is thread-safe. The indexer is the simplest way to unconditionally add / overwrite a value. Sometimes you’ll want to use other … Read more

System.Text.Json – How to serialize non-public properties

By default, System.Text.Json.JsonSerializer only serializes public properties. If you want to serialize non-public properties, you have two options: In this article, I’ll show examples of both approaches for handling non-public properties. Updated 2022-02-22 to explain the new JsonInclude attribute added in .NET 5. Write a custom JSON converter to serialize non-public properties When the built-in … Read more

C# – Hex string to byte array

This article shows code for converting a hex string to a byte array, unit tests, and a speed comparison. First, this diagram shows the algorithm for converting a hex string to a byte array. To convert a hex string to a byte array, you need to loop through the hex string and convert two characters … Read more

KeyNotFoundException: The given key was not present in the dictionary

Problem The following exception is thrown when you try to get a value from a dictionary using a key that doesn’t exist in the dictionary: KeyNotFoundException: ‘The given key was not present in the dictionary.’ Consider the following the example of initializing a dictionary with a few key/value pairs, and then trying to access non-existent … Read more