using System.Collections.Generic;
using System.Diagnostics;
private static Random random = new Random();
public static string RandomString(int length)
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
int numberOfElements = 10 * 1000;
List<string> list = new List<string>(numberOfElements);
for (int i = 0; i < numberOfElements; i++)
list.Add(RandomString(20));
HashSet<string> hash = new HashSet<string>(list);
Dictionary<string, string> dictionary = list.ToDictionary(x => x, x => x);
Stopwatch stopwatch = Stopwatch.StartNew();
foreach (string s in list)
hash.TryGetValue(s, out string xx);
long hashTime = stopwatch.ElapsedMilliseconds;
foreach (string s in list)
string singleOrDefault = list.SingleOrDefault(x => x == s);
long listTime = stopwatch.ElapsedMilliseconds;
foreach (string s in list)
dictionary.TryGetValue(s, out string xx);
long dicTime = stopwatch.ElapsedMilliseconds;
(hashTime + " " + dicTime + " " + listTime).Should().Be("");