Calculating entropy of a string We're calculating entropy of a string a few places in Stack Overflow as a signifier of low quality.

I whipped up this simple method which counts unique characters in a string, but it is quite literally the first thing that popped into my head. It's the "dumbest thing that works".

/// &lt;summary&gt;
/// returns the # of unique characters in a string as a rough 
/// measurement of entropy
/// &lt;/summary&gt;
public static int Entropy(this string s)
{
  var d = new Dictionary&lt;char, bool&gt;();
  foreach (char c in s)
      if (!d.ContainsKey(c)) d.Add(c, true);
  return d.Count();
}


Is there a better / more elegant / more accurate way to calculate the entropy of a string?

Efficiency is also good, though we never call this on large strings so it is not a huge concern.
Won't this work too?

string name = "lltt";
int uniqueCharacterCount = name.Distinct().Count();


will return 2