瀏覽代碼

Streamline object.Equals implementation.

Dariusz Komosinski 6 年之前
父節點
當前提交
58af056440

+ 6 - 9
src/Avalonia.Visuals/CornerRadius.cs

@@ -82,15 +82,12 @@ namespace Avalonia
             // ReSharper restore CompareOfFloatsByEqualityOperator
         }
 
-        public override bool Equals(object obj)
-        {
-            if (!(obj is CornerRadius))
-            {
-                return false;
-            }
-
-            return Equals((CornerRadius)obj);
-        }
+        /// <summary>
+        /// Returns a boolean indicating whether the given Object is equal to this corner radius instance.
+        /// </summary>
+        /// <param name="obj">The Object to compare against.</param>
+        /// <returns>True if the Object is equal to this corner radius; False otherwise.</returns>
+        public override bool Equals(object obj) => obj is CornerRadius other && Equals(other);
 
         public override int GetHashCode()
         {

+ 1 - 9
src/Avalonia.Visuals/Matrix.cs

@@ -250,15 +250,7 @@ namespace Avalonia
         /// </summary>
         /// <param name="obj">The Object to compare against.</param>
         /// <returns>True if the Object is equal to this matrix; False otherwise.</returns>
-        public override bool Equals(object obj)
-        {
-            if (!(obj is Matrix))
-            {
-                return false;
-            }
-
-            return Equals((Matrix)obj);
-        }
+        public override bool Equals(object obj) => obj is Matrix other && Equals(other);
 
         /// <summary>
         /// Returns the hash code for this instance.

+ 1 - 9
src/Avalonia.Visuals/Media/PixelPoint.cs

@@ -147,15 +147,7 @@ namespace Avalonia
         /// <returns>
         /// True if <paramref name="obj"/> is a point that equals the current point.
         /// </returns>
-        public override bool Equals(object obj)
-        {
-            if (obj is PixelPoint other)
-            {
-                return Equals(other);
-            }
-
-            return false;
-        }
+        public override bool Equals(object obj) => obj is PixelPoint other && Equals(other);
 
         /// <summary>
         /// Returns a hash code for a <see cref="PixelPoint"/>.

+ 1 - 9
src/Avalonia.Visuals/Media/PixelRect.cs

@@ -211,15 +211,7 @@ namespace Avalonia
         /// </summary>
         /// <param name="obj">The object to compare against.</param>
         /// <returns>True if the object is equal to this rectangle; false otherwise.</returns>
-        public override bool Equals(object obj)
-        {
-            if (obj is PixelRect other)
-            {
-                return Equals(other);
-            }
-
-            return false;
-        }
+        public override bool Equals(object obj) => obj is PixelRect other && Equals(other);
 
         /// <summary>
         /// Returns the hash code for this instance.

+ 1 - 9
src/Avalonia.Visuals/Media/PixelSize.cs

@@ -97,15 +97,7 @@ namespace Avalonia
         /// <returns>
         /// True if <paramref name="obj"/> is a size that equals the current size.
         /// </returns>
-        public override bool Equals(object obj)
-        {
-            if (obj is PixelSize other)
-            {
-                return Equals(other);
-            }
-
-            return false;
-        }
+        public override bool Equals(object obj) => obj is PixelSize other && Equals(other);
 
         /// <summary>
         /// Returns a hash code for a <see cref="PixelSize"/>.

+ 1 - 9
src/Avalonia.Visuals/Point.cs

@@ -207,15 +207,7 @@ namespace Avalonia
         /// <returns>
         /// True if <paramref name="obj"/> is a point that equals the current point.
         /// </returns>
-        public override bool Equals(object obj)
-        {
-            if (!(obj is Point))
-            {
-                return false;
-            }
-
-            return Equals((Point)obj);
-        }
+        public override bool Equals(object obj) => obj is Point other && Equals(other);
 
         /// <summary>
         /// Returns a hash code for a <see cref="Point"/>.

+ 1 - 9
src/Avalonia.Visuals/Rect.cs

@@ -319,15 +319,7 @@ namespace Avalonia
         /// </summary>
         /// <param name="obj">The object to compare against.</param>
         /// <returns>True if the object is equal to this rectangle; false otherwise.</returns>
-        public override bool Equals(object obj)
-        {
-            if (!(obj is Rect))
-            {
-                return false;
-            }
-
-            return Equals((Rect)obj);
-        }
+        public override bool Equals(object obj) => obj is Rect other && Equals(other);
 
         /// <summary>
         /// Returns the hash code for this instance.

+ 1 - 4
src/Avalonia.Visuals/RelativePoint.cs

@@ -107,10 +107,7 @@ namespace Avalonia
         /// </summary>
         /// <param name="obj">The other object.</param>
         /// <returns>True if the objects are equal, otherwise false.</returns>
-        public override bool Equals(object obj)
-        {
-            return (obj is RelativePoint) && Equals((RelativePoint)obj);
-        }
+        public override bool Equals(object obj) => obj is RelativePoint other && Equals(other);
 
         /// <summary>
         /// Checks if the <see cref="RelativePoint"/> equals another point.

+ 1 - 4
src/Avalonia.Visuals/RelativeRect.cs

@@ -116,10 +116,7 @@ namespace Avalonia
         /// </summary>
         /// <param name="obj">The other object.</param>
         /// <returns>True if the objects are equal, otherwise false.</returns>
-        public override bool Equals(object obj)
-        {
-            return (obj is RelativeRect) && Equals((RelativeRect)obj);
-        }
+        public override bool Equals(object obj) => obj is RelativeRect other && Equals(other);
 
         /// <summary>
         /// Checks if the <see cref="RelativeRect"/> equals another rectangle.

+ 1 - 9
src/Avalonia.Visuals/Size.cs

@@ -211,15 +211,7 @@ namespace Avalonia
         /// <returns>
         /// True if <paramref name="obj"/> is a size that equals the current size.
         /// </returns>
-        public override bool Equals(object obj)
-        {
-            if (!(obj is Size))
-            {
-                return false;
-            }
-
-            return Equals((Size)obj);
-        }
+        public override bool Equals(object obj) => obj is Size other && Equals(other);
 
         /// <summary>
         /// Returns a hash code for a <see cref="Size"/>.

+ 1 - 9
src/Avalonia.Visuals/Thickness.cs

@@ -248,15 +248,7 @@ namespace Avalonia
         /// <returns>
         /// True if <paramref name="obj"/> is a size that equals the current size.
         /// </returns>
-        public override bool Equals(object obj)
-        {
-            if (!(obj is Thickness))
-            {
-                return false;
-            }
-
-            return Equals((Thickness)obj);
-        }
+        public override bool Equals(object obj) => obj is Thickness other && Equals(other);
 
         /// <summary>
         /// Returns a hash code for a <see cref="Thickness"/>.

+ 1 - 8
src/Avalonia.Visuals/Vector.cs

@@ -138,7 +138,6 @@ namespace Avalonia
         /// </summary>
         /// <param name="other">The other vector.</param>
         /// <returns>True if vectors are nearly equal.</returns>
-        [Pure]
         public bool NearlyEquals(Vector other)
         {
             const float tolerance = float.Epsilon;
@@ -146,13 +145,7 @@ namespace Avalonia
             return Math.Abs(_x - other._x) < tolerance && Math.Abs(_y - other._y) < tolerance;
         }
 
-        public override bool Equals(object obj)
-        {
-            if (ReferenceEquals(null, obj))
-                return false;
-
-            return obj is Vector vector && Equals(vector);
-        }
+        public override bool Equals(object obj) => obj is Vector other && Equals(other);
 
         public override int GetHashCode()
         {

+ 1 - 9
src/Avalonia.Visuals/VisualTree/TransformedBounds.cs

@@ -57,15 +57,7 @@ namespace Avalonia.VisualTree
             return Bounds == other.Bounds && Clip == other.Clip && Transform == other.Transform;
         }
 
-        public override bool Equals(object obj)
-        {
-            if (obj is null)
-            {
-                return false;
-            }
-
-            return obj is TransformedBounds other && Equals(other);
-        }
+        public override bool Equals(object obj) => obj is TransformedBounds other && Equals(other);
 
         public override int GetHashCode()
         {