using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
public class ConcurrentReadDictionary<TKey, TValue>
private Dictionary<TKey, TValue> m_data = new Dictionary<TKey, TValue>();
public TValue GetOrAdd(TKey key, TValue value)
if (!m_data.TryGetValue(key, out val))
if (!m_data.TryGetValue(key, out val))
m_data[key] = val = value;
public bool TryGetValue(TKey key, out TValue value) { return m_data.TryGetValue(key, out value); }
public static void Main()
const int loopCount = 50000;
var sw = Stopwatch.StartNew();
var concurrentReadDictionary = new ConcurrentReadDictionary<int, int>();
for (var i = 0; i < loopCount; i++)
for (var j = 0; j < count; j++)
var k = concurrentReadDictionary.GetOrAdd(j, j);
Console.WriteLine("ConcurrentReadDictionary for {0:#,##0} writes and {1:#,##0} took {2:#,##0} ms", count, loopCount, sw.ElapsedMilliseconds);
var concurrentDictionary = new ConcurrentDictionary<int, int>();
for (var i = 0; i < loopCount; i++)
for (var j = 0; j < count; j++)
var k = concurrentDictionary.GetOrAdd(j, j);
Console.WriteLine("ConcurrentReadDictionary for {0:#,##0} writes and {1:#,##0} took {2:#,##0} ms", count, loopCount, sw.ElapsedMilliseconds);