Size Struct

Represents a non-negative two-dimensional size, in pixels.

public readonly struct Size : IEquatable<Size>

Remarks

A Size can be empty (zero width or height). APIs that create or resize images reject empty sizes with an ArgumentOutOfRangeException instead of silently clamping them.

Implements

Constructors

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

Size(int, int)

Size.cs

Initializes a new instance of the Size struct.

public Size(int width, int height)

Parameters

width int
The width, in pixels. Must be zero or positive.
height int
The height, in pixels. Must be zero or positive.

Exceptions

ArgumentOutOfRangeException
width or height is negative.

Properties

NameDescription
AreaGets the number of pixels covered by this size, computed without overflow.
EmptyGets a size whose width and height are zero.
HeightGets the height, in pixels.
IsEmptyGets a value indicating whether the width or the height is zero.
WidthGets the width, in pixels.

Gets the number of pixels covered by this size, computed without overflow.

public long Area { get; }

Property value

long

Empty

Size.cs

Gets a size whose width and height are zero.

public static Size Empty { get; }

Property value

Size

Height

Size.cs

Gets the height, in pixels.

public int Height { get; }

Property value

int

IsEmpty

Size.cs

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

public bool IsEmpty { get; }

Property value

bool

Width

Size.cs

Gets the width, in pixels.

public int Width { get; }

Property value

int

Methods

NameDescription
Deconstruct(int, int)Deconstructs the size into its width and height.
Equals(Size)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.
GetHashCode()Returns the hash code for this instance.
ToString()Returns the size formatted as {Width}x{Height}.

Deconstruct(int, int)

Size.cs

Deconstructs the size into its width and height.

public void Deconstruct(out int width, out int height)

Parameters

width int
The width, in pixels.
height int
The height, in pixels.

Equals overloads

Equals(Size)

Size.cs

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

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

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

Equals(object?)

Size.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.

GetHashCode()

Size.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.

ToString()

Size.cs

Returns the size formatted as {Width}x{Height}.

public override string ToString()

Returns

string A string representation of the size.

Operators

NameDescription
operator ==(Size, Size)Compares two sizes for equality.
operator !=(Size, Size)Compares two sizes for inequality.

operator ==(Size, Size)

Size.cs

Compares two sizes for equality.

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

Parameters

left Size
The first size.
right Size
The second size.

Returns

bool true if both sizes are equal; otherwise false.

operator !=(Size, Size)

Size.cs

Compares two sizes for inequality.

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

Parameters

left Size
The first size.
right Size
The second size.

Returns

bool true if the sizes differ; otherwise false.