/// <summary> /// Compares strings ignoring case and culture
/// </summary> public class IgnoreCaseEqualityComparer : IEqualityComparer<string> {
#region Members
private const int _multiplier = 89;
#endregion
#region IEqualityComparer Implementation
public bool Equals(string x, string y)
{
if (x == null || y == null)
return false;
return x.Equals(y, StringComparison.InvariantCultureIgnoreCase);
}
public int GetHashCode(string obj)
{
int hashCode = 0;
// Fault value for empty strings
if (String.IsNullOrEmpty(obj))
return hashCode;
// Get length to determine first and last chars
char firstLetter = obj.First();
char lastLetter = obj.Last();
// Compute hash code from two characters
int part1 = firstLetter + obj.Length;
hashCode = (_multiplier * part1) + lastLetter + obj.Length;
return hashCode;
}
#endregion
}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.