65.9K
CodeProject is changing. Read more.
Home

Creating Key-Value pair in a dictionary if key is not available

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Sep 27, 2013

CPOL
viewsIcon

9220

The ConcurrentDictionary type resides in System.Collections.Concurrent, introduced in .NET 4.0. It makes adding, removing and updating values in a lookup table on multiple threads easier.

Introduction

The ConcurrentDictionary type resides in System.Collections.Concurrent, introduced in .NET 4.0. It makes adding, removing, and updating values in a lookup table on multiple threads easier.

Background

How to add a Key-Value pair into a dictionary if the key is not available in the dictionary?

Using the code

The ConcurrentDictionary type resides in System.Collections.Concurrent, introduced in .NET 4.0. It makes adding, removing and updating values in a lookup table on multiple threads easier.

Applications of ConcurrentDictionary:

  1. Creates Key-Value pair hashtable.
  2. If key is not available in the dictionary adds value into the dictionary.

Code snippet for getting value for a new key.

Customized GetOrAddMethod to get the status of newly added Key-Value pair.