Create a new type that encapsulates four values. For instance:
public sealed class User { private readonly string name; private readonly string login; private readonly int points; private readonly WindowsIdentity identity; public User(string name, string login, int points, WindowsIdentity identity) { this.name = name; this.login = login; this.points = points; this.identity = identity; } public string Name { get { return name; } } public string Login { get { return login; } } public int Points { get { return points; } } public WindowsIdentity Identity { get { return identity; } } public override bool Equals(object other) { User otherUser = other as User; if (otherUser == null) { return false; } return name == otherUser.name && login == otherUser.login && points == otherUser.points && identity.Equals(otherUser.identity); } public override int GetHashCode() { int hash = 17; hash = hash * 31 + name.GetHashCode(); hash = hash * 31 + login.GetHashCode(); hash = hash * 31 + points.GetHashCode(); hash = hash * 31 + identity.GetHashCode(); return hash; } }
Note that this assumes that WindowsIdentity overrides Equals and GetHashCode accordingly - or that you are happy with the equality of the reference type.
This approach is much more reliable than any of your suggestions - for example, in your first approach, two pairs of lines "xy", "z" and "x", "yz" ultimately form the same cache key (if int and identity match), while they should not. The second approach is likely to lead to random hash collisions.
source share