Rectangle Struct

Represents an axis-aligned rectangle of pixels with exclusive Rectangle.Right and Rectangle.Bottom bounds.

public readonly struct Rectangle : IEquatable<Rectangle>

Remarks

The constructor guarantees that X + Width and Y + Height are representable as Int32, so Rectangle.Right and Rectangle.Bottom never overflow.

Implements

Constructors

NameDescription
Rectangle(Point, Size)Initializes a new instance of the Rectangle struct.
Rectangle(int, int, int, int)Initializes a new instance of the Rectangle struct.

Rectangle overloads

Rectangle(Point, Size)

Rectangle.cs

Initializes a new instance of the Rectangle struct.

public Rectangle(Point location, Size size)
Parameters
location Point
The top-left corner (inclusive).
size Size
The size.
Exceptions
ArgumentOutOfRangeException
The exclusive right or bottom bound overflows Int32.

Rectangle(int, int, int, int)

Rectangle.cs

Initializes a new instance of the Rectangle struct.

public Rectangle(int x, int y, int width, int height)
Parameters
x int
The left coordinate (inclusive).
y int
The top coordinate (inclusive).
width int
The width. Must be zero or positive.
height int
The height. Must be zero or positive.
Exceptions
ArgumentOutOfRangeException
width or height is negative, or the exclusive right or bottom bound overflows Int32.

Properties

NameDescription
BottomGets the exclusive bottom bound (Y + Height).
EmptyGets an empty rectangle located at the origin.
HeightGets the height.
IsEmptyGets a value indicating whether the width or the height is zero.
LeftGets the left coordinate (inclusive). Same as Rectangle.X.
LocationGets the top-left corner.
RightGets the exclusive right bound (X + Width).
SizeGets the size.
TopGets the top coordinate (inclusive). Same as Rectangle.Y.
WidthGets the width.
XGets the left coordinate (inclusive).
YGets the top coordinate (inclusive).

Gets the exclusive bottom bound (Y + Height).

public int Bottom { get; }

Property value

int

Gets an empty rectangle located at the origin.

public static Rectangle Empty { get; }

Property value

Rectangle

Gets the height.

public int Height { get; }

Property value

int

Gets a value indicating whether the width or the height is zero.

public bool IsEmpty { get; }

Property value

bool

Gets the left coordinate (inclusive). Same as Rectangle.X.

public int Left { get; }

Property value

int

Location

Rectangle.cs

Gets the top-left corner.

public Point Location { get; }

Property value

Point

Gets the size.

public Size Size { get; }

Property value

Size

Gets the top coordinate (inclusive). Same as Rectangle.Y.

public int Top { get; }

Property value

int

Gets the width.

public int Width { get; }

Property value

int

Gets the left coordinate (inclusive).

public int X { get; }

Property value

int

Gets the top coordinate (inclusive).

public int Y { get; }

Property value

int

Methods

NameDescription
Contains(Point)Determines whether the specified point is inside this rectangle.
Contains(Rectangle)Determines whether the specified rectangle is entirely inside this rectangle.
Equals(Rectangle)Indicates whether the current object is equal to another object of the same type.
Equals(object?)Indicates whether this instance and a specified object are equal.
FromLTRB(int, int, int, int)Creates a rectangle from its inclusive left/top and exclusive right/bottom bounds.
GetHashCode()Returns the hash code for this instance.
Intersect(Rectangle, Rectangle)Returns the intersection of two rectangles, or Rectangle.Empty if they do not overlap.
IntersectsWith(Rectangle)Determines whether this rectangle overlaps the specified rectangle by at least one pixel.
ToString()Returns the rectangle formatted as {X},{Y} {Width}x{Height}.

Contains overloads

Contains(Point)

Rectangle.cs

Determines whether the specified point is inside this rectangle.

public bool Contains(Point point)
Parameters
point Point
The point to test.
Returns

bool true if the point is inside; otherwise false.

Contains(Rectangle)

Rectangle.cs

Determines whether the specified rectangle is entirely inside this rectangle.

public bool Contains(Rectangle rectangle)
Parameters
rectangle Rectangle
The rectangle to test.
Returns

bool true if rectangle is entirely contained; otherwise false.

Equals overloads

Equals(Rectangle)

Rectangle.cs

Indicates whether the current object is equal to another object of the same type.

public bool Equals(Rectangle other)
Parameters
other Rectangle
An object to compare with this object.
Returns

bool true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Rectangle.cs

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current instance.
Returns

bool true if obj and this instance are the same type and represent the same value; otherwise, false.

FromLTRB(int, int, int, int)

Rectangle.cs

Creates a rectangle from its inclusive left/top and exclusive right/bottom bounds.

public static Rectangle FromLTRB(int left, int top, int right, int bottom)

Parameters

left int
The left coordinate (inclusive).
top int
The top coordinate (inclusive).
right int
The right coordinate (exclusive). Must be greater than or equal to left.
bottom int
The bottom coordinate (exclusive). Must be greater than or equal to top.

Returns

Rectangle The rectangle.

Exceptions

ArgumentOutOfRangeException
A bound is inverted or the resulting size overflows Int32.

GetHashCode()

Rectangle.cs

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int A 32-bit signed integer that is the hash code for this instance.

Intersect(Rectangle, Rectangle)

Rectangle.cs

Returns the intersection of two rectangles, or Rectangle.Empty if they do not overlap.

public static Rectangle Intersect(Rectangle a, Rectangle b)

Parameters

a Rectangle
The first rectangle.
b Rectangle
The second rectangle.

Returns

Rectangle The intersection.

IntersectsWith(Rectangle)

Rectangle.cs

Determines whether this rectangle overlaps the specified rectangle by at least one pixel.

public bool IntersectsWith(Rectangle rectangle)

Parameters

rectangle Rectangle
The rectangle to test.

Returns

bool true if the rectangles overlap; otherwise false (always for an empty rectangle).

ToString()

Rectangle.cs

Returns the rectangle formatted as {X},{Y} {Width}x{Height}.

public override string ToString()

Returns

string A string representation of the rectangle.

Operators

NameDescription
operator ==(Rectangle, Rectangle)Compares two rectangles for equality.
operator !=(Rectangle, Rectangle)Compares two rectangles for inequality.

operator ==(Rectangle, Rectangle)

Rectangle.cs

Compares two rectangles for equality.

public static bool operator ==(Rectangle left, Rectangle right)

Parameters

left Rectangle
The first rectangle.
right Rectangle
The second rectangle.

Returns

bool true if both rectangles are equal; otherwise false.

operator !=(Rectangle, Rectangle)

Rectangle.cs

Compares two rectangles for inequality.

public static bool operator !=(Rectangle left, Rectangle right)

Parameters

left Rectangle
The first rectangle.
right Rectangle
The second rectangle.

Returns

bool true if the rectangles differ; otherwise false.