Namespace: System.Drawing
Structure: Size
Represents an ordered pair of 2D integer coordinates that define a rectangle or, in some contexts, the dimensions of an object. The X coordinate is horizontal and the Y coordinate is vertical.
The Size structure is a fundamental type in the System.Drawing namespace, used extensively for defining and manipulating graphical dimensions. It's a value type, meaning it's stored directly on the stack or inline within containing objects, contributing to performance. Its immutability (once created, its width and height cannot be changed) ensures predictable behavior.
public struct Size : IEquatable<Size>
Size(int width, int height)Initializes a new instance of the Size structure with the specified width and height.
public Size(int width, int height)
EmptyA static readonly field that represents an uninitialized Size structure. This field is constant and has a Width and Height of 0.
public static readonly Size Empty
HeightThe height of this Size structure.
public int Height
WidthThe width of this Size structure.
public int Width
The Size structure has no public properties beyond its fields, which can be accessed directly.
Equals(object obj)Determines whether the specified object is a Size structure and is equivalent to this Size structure.
public override bool Equals(object obj)
Equals(Size size)Indicates whether the current object is equal to another object of the same type.
public bool Equals(Size size)
GetHashCode()Returns the hash code for this Size structure for use in hashing algorithms and collections.
public override int GetHashCode()
ToString()Converts this Size structure to a human-readable string.
public override string ToString()
== (Equality)Compares two Size structures for equality.
public static bool operator ==(Size size1, Size size2)
!= (Inequality)Compares two Size structures for inequality.
public static bool operator !=(Size size1, Size size2)
The Size structure is a value type, meaning it is represented by its value. When you pass a Size to a method, a copy of the structure is passed. This differs from reference types, where only a reference is passed.
Instances of Size can be added to or subtracted from Point structures to adjust their positions.
When comparing two Size structures, the Equals method and the == operator check if both the Width and Height members are equal.
The following code example demonstrates how to create and use the Size structure.
using System.Drawing;
public class SizeExample
{
public static void Main(string[] args)
{
// Create a Size object
Size imageSize = new Size(800, 600);
// Access Width and Height
int width = imageSize.Width;
int height = imageSize.Height;
System.Console.WriteLine($"Image dimensions: {width}x{height}");
// Create another Size object
Size smallerSize = new Size(100, 50);
// Compare two Size objects
if (imageSize != smallerSize)
{
System.Console.WriteLine("The image size is different from the smaller size.");
}
// Use the static Empty field
Size emptySize = Size.Empty;
System.Console.WriteLine($"Empty size: {emptySize.Width}x{emptySize.Height}");
// Example of adding Size to a Point (conceptual, requires Point)
// Point startPoint = new Point(10, 10);
// Size offset = new Size(20, 30);
// Point endPoint = startPoint + offset; // This operation is defined for Point and Size
// System.Console.WriteLine($"End point: {endPoint.X},{endPoint.Y}");
}
}