I use the following structure in a web service where performance is critical. It is used for making joins between domain data objects, the key is made of two integers. The idea is to fit the two 32 bits integer in a long (64 bits).
Before using that structure, I used Tuple<int, int>
which was really slower.
Is there any additional optimization to be made? (e.g.: addtional override, unneeded cast etc...)
public struct CombinedKey : IEquatable<CombinedKey>
{
private readonly long value;
public CombinedKey(int item1, int item2)
{
unchecked
{
value = (long)item1 << 32 | (uint)item2;
}
}
public override int GetHashCode()
{
return (int)value ^ (int)(value >> 32);
}
public bool Equals(CombinedKey other)
{
return this.value == other.value;
}
public int Item1
{
get
{
return (int)(this.value >> 32);
}
}
public int Item2
{
get
{
return (int)(this.value & 0xffffffff);
}
}
}
item1
anditem2
as two fields? \$\endgroup\$long
? \$\endgroup\$long
in code it can mean lot of things,CombinedKey
is obvious. \$\endgroup\$