Implement IEquatable on structs.

Any struct which overrides object.Equals(object obj) should implement IEquatable<T> to gain a more efficient Equals(T other) overload. This overload will be used by hashing collections like Dictionary which enables them to check equality without boxing the struct.
This commit is contained in:
RoosterDragon
2019-09-14 00:33:27 +01:00
committed by teinarss
parent 4a609bbee8
commit 6c9fbd40dc
6 changed files with 33 additions and 17 deletions

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
@@ -16,7 +17,7 @@ namespace OpenRA
{
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")]
[StructLayout(LayoutKind.Sequential)]
public struct float3
public struct float3 : IEquatable<float3>
{
public readonly float X, Y, Z;
public float2 XY { get { return new float2(X, Y); } }
@@ -47,6 +48,11 @@ namespace OpenRA
public static bool operator !=(float3 me, float3 other) { return !(me == other); }
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }
public bool Equals(float3 other)
{
return other == this;
}
public override bool Equals(object obj)
{
var o = obj as float3?;