A Dictionary is complex. Some actions may be slower than you think. The Count and Clear members both seem simple. But the Count property is much faster than Clear.
using System;
using System.Collections.Generic;
using System.Diagnostics;
const int _max = 100000000;
var dict = new Dictionary<string, string>();
var s1 = Stopwatch.StartNew();
// Version 1: use Clear.
for (int i = 0; i < _max; i++)
{
dict.Clear();
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: use Clear if needed.
for (int i = 0; i < _max; i++)
{
if (dict.Count > 0)
{
dict.Clear();
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString(
"0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString(
"0.00 ns"));
1.88 ns Always call Clear
1.04 ns Call Clear if Count > 0