There isn't a tool or library for that, however you can refactor this code to be less verbose. For example, the code as is could be simplified using the Distinct method.
foreach (var str in arbitraryStringCollection.Distinct())
{
ProcessString(str)
}
You could further refactor it using some sort of ForEach
extension method, or refactor the entire thing into an extension method.
Alternatively, if your requirements are slightly different (e.g. you want to keep dict
for the lifetime of the application), then this could be refactored in a slightly different way, e.g.
HashSet<string> dict = new HashSet<string>();
foreach(string str in arbitraryStringCollection)
{
dict.DoOnce(str, ProcessString);
}
// Re-usable extension method)
public static class ExtensionMethods
{
public static void DoOnce<T>(this ISet<T> set, T value, Action<T> action)
{
if (!set.Contains(value))
{
action(value);
set.Add(value);
}
}
}