ConcurrentDictionary Vs Dictionary

CSharp · Console

/Program.cs

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

public class Program
{
    static ConcurrentDictionary<string, int> _concurrent = new ConcurrentDictionary<string, int>();
	//static Dictionary<string, int> _dictionary = new Dictionary<string, int>();

    public static void Main()
    {
		Thread thread1 = new Thread(new ThreadStart(AddToDict));
		Thread thread2 = new Thread(new ThreadStart(AddToDict));
		thread1.Start();
		thread2.Start();
		thread1.Join();
		thread2.Join();
		Console.WriteLine("Average: {0}", _concurrent.Values.Average());
		//Console.WriteLine("Average: {0}", _dictionary.Values.Average());
    }

    static void AddToDict()
    {
		for (int i = 0; i < 6000; i++)
		{
			_concurrent.TryAdd(i.ToString(), i);
			//_dictionary.Add(i.ToString(), i);
		}
    }
}
;