Rect.Equals Method

Definition

Indicates whether two rectangles are equal.

Overloads

Equals(Rect, Rect)

Indicates whether the specified rectangles are equal.

Equals(Object)

Indicates whether the specified object is equal to the current rectangle.

Equals(Rect)

Indicates whether the specified rectangle is equal to the current rectangle.

Equals(Rect, Rect)

Indicates whether the specified rectangles are equal.

C#
public static bool Equals(System.Windows.Rect rect1, System.Windows.Rect rect2);

Parameters

rect1
Rect

The first rectangle to compare.

rect2
Rect

The second rectangle to compare.

Returns

true if the rectangles have the same Location and Size values; otherwise, false.

Examples

The following example shows how to use the Equals(Rect, Rect) method to determine if one rectangle is equal to another.

C#
private bool rectEqualsExample2()
{
    // Create a rectangle.
    Rect myRectangle1 = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. 
    myRectangle1.Location = new Point(10, 5);

    // Set the Size property of the rectangle with a width of 200
    // and a height of 50.
    myRectangle1.Size = new Size(200, 50);

    // Create second rectangle to compare to the first.
    Rect myRectangle2 = new Rect();
    myRectangle2.Location = new Point(10, 5);
    myRectangle2.Size = new Size(200, 50);

    // Using the Equals method, see if the second rectangle is 
    // the same as the first rectangle. doesEqual is true because
    // both rectangles are exactly the same in that they both have the 
    // same location and size.
    bool doesEqual = Rect.Equals(myRectangle1, myRectangle2);

    return doesEqual;
}

Remarks

This operation tests for object equality.

In this comparison, two instances of Double.NaN are considered equal.

Note

A rectangle's position and dimensions are described by Double values. Because Double values can lose precision when operated upon, a comparison between two values that are logically equal might fail.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Equals(Object)

Indicates whether the specified object is equal to the current rectangle.

C#
public override bool Equals(object o);

Parameters

o
Object

The object to compare to the current rectangle.

Returns

true if o is a Rect and has the same Location and Size values as the current rectangle; otherwise, false.

Examples

The following example shows how to use the Equals(Object) method to determine if a rectangle is equal to a specified object.

C#
private bool rectEqualsExample1()
{
    // Create a rectangle.
    Rect myRectangle1 = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. 
    myRectangle1.Location = new Point(10, 5);

    // Set the Size property of the rectangle with a width of 200
    // and a height of 50.
    myRectangle1.Size = new Size(200, 50);

    // Create second rectangle to compare to the first.
    Rect myRectangle2 = new Rect();
    myRectangle2.Location = new Point(10, 5);
    myRectangle2.Size = new Size(200, 50);

    // Using the Equals method, see if the second rectangle is the
    // same as the first rectangle. doesEqual is true because both
    // rectangles are exactly the same with respect to location and size. 
    bool doesEqual = myRectangle1.Equals(myRectangle2);

    return doesEqual;
}

Remarks

This operation tests for object equality.

In this comparison, two instances of Double.NaN are considered equal.

Note

A rectangle's position and dimensions are described by Double values. Because Double values can lose precision when operated upon, a comparison between two values that are logically equal might fail.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Equals(Rect)

Indicates whether the specified rectangle is equal to the current rectangle.

C#
public bool Equals(System.Windows.Rect value);

Parameters

value
Rect

The rectangle to compare to the current rectangle.

Returns

true if the specified rectangle has the same Location and Size values as the current rectangle; otherwise, false.

Examples

The following example shows how to use the Equals(Rect) method to determine if a rectangle is equal to a specified rectangle.

C#
private bool rectEqualsExample1()
{
    // Create a rectangle.
    Rect myRectangle1 = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. 
    myRectangle1.Location = new Point(10, 5);

    // Set the Size property of the rectangle with a width of 200
    // and a height of 50.
    myRectangle1.Size = new Size(200, 50);

    // Create second rectangle to compare to the first.
    Rect myRectangle2 = new Rect();
    myRectangle2.Location = new Point(10, 5);
    myRectangle2.Size = new Size(200, 50);

    // Using the Equals method, see if the second rectangle is the
    // same as the first rectangle. doesEqual is true because both
    // rectangles are exactly the same with respect to location and size. 
    bool doesEqual = myRectangle1.Equals(myRectangle2);

    return doesEqual;
}

Remarks

This operation tests for object equality.

In this comparison, two instances of Double.NaN are considered equal.

Note

A rectangle's position and dimensions are described by Double values. Because Double values can lose precision when operated upon, a comparison between two values that are logically equal might fail.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10