Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / C# 4.0
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
27 Sep 2013CPOL 9.1K   4  
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.

Image 1

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

Image 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --