Rect.Equality Operator

Compares two rectangles for exact equality.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)

Public Shared Operator = ( _
	rect1 As Rect, _
	rect2 As Rect _
) As Boolean


Type: System.Windows.Rect

The first rectangle to compare.

Type: System.Windows.Rect

The second rectangle to compare.

Return Value

Type: System.Boolean
true if the rectangles have the same Location and Size values; otherwise, false.

This operation tests for object equality.

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


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.

The following example shows how to use the Equality operator to determine if two rectangles are exactly equal.

		Private Function overloadedEqualityOperatorExample() As Boolean
			' Initialize new rectangle.
			Dim myRectangle As New Rect()

			' The Location property specifies the coordinates of the upper left-hand 
			' corner of the rectangle. 
			myRectangle.Location = New Point(10, 5)

			' Set the Size property of the rectangle with a width of 200
			' and a height of 50.
			myRectangle.Size = New Size(200, 50)

			' Create second rectangle to compare to the first.
			Dim myRectangle2 As New Rect()
			myRectangle2.Location = New Point(0, 0)
			myRectangle2.Size = New Size(200, 50)

			' Check if the two Rects are exactly equal using the overloaded equality operator.
			' areEqual is false because although the size of each rectangle is the same,
			' the locations are different.
			Dim areEqual As Boolean = (myRectangle = myRectangle2)

			' Returns false.
			Return areEqual

		End Function

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft