Size Structure

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.

Table of Contents

Introduction

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.

Syntax

public struct Size : IEquatable<Size>

Constructors

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)

Fields

Empty

A 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

Height

The height of this Size structure.

public int Height

Width

The width of this Size structure.

public int Width

Properties

The Size structure has no public properties beyond its fields, which can be accessed directly.

Methods

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()

Operators

== (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)

Remarks

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.

Example

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}");
    }
}