Get value in case-insensitive HashSet <string>
I am case insensitive HashSet<string>:
private HashSet<string> a = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
I am curious if I can now extract the string in the actual case. Pseudocode of what I need:
return a.Contains(word) ? a[word] : null;
(just pseudo code, it will not work)
For example, I have the string " TestXxX " in a HashSet. I need a code that receives "testxxx" (or "tEsTXXx") as input and returns " TestXxX ".
My current workaround is to use Dictionary<string,string>instead and put the same value for the key and value. This is obviously not elegant and consumes 2x of memory as required.
+4
2
public class Keyed : KeyedCollection<string, string>
{
public Keyed(IEqualityComparer<string> comparer) : base(comparer)
{
}
protected override string GetKeyForItem(string item)
{
return item;
}
}
:
var keyed = new Keyed(StringComparer.InvariantCultureIgnoreCase);
keyed.Add("TestXxX");
Console.WriteLine(keyed["tEsTXXx"]);
+6