Often we do not need to test the return value of TryAdd. And by ignoring the return value, our programs can be slightly cleaner and simpler.
using System;
using System.Collections.Generic;
var cache = new Dictionary<string, bool>();
// Step 1: Add the items with TryAdd.
// ... We can ignore the return value if not needed.
var items = new string[] {
"bird",
"bird",
"frog",
"fish",
"frog" };
foreach (var item in items)
{
cache.TryAdd(item, true);
}
// Step 2: display the dictionary.
foreach (var pair in cache)
{
Console.WriteLine(pair);
}
[bird, True]
[frog, True]
[fish, True]