Home
C#
Size Type Example
This page was last reviewed on Jan 4, 2024.
Dot Net Perls
Size, SizeF. How can you create, read and modify the Size struct? The Size struct is found in the System.Drawing namespace. A size represents a rectangular measurement.
Point
Property notes. We set the Width and Height properties on Size. We also see if it is empty with IsEmpty. As a struct, it is copied by value like other value types (such as int).
struct
Size. First, any Size instance that has zero height and zero width is empty: IsEmpty will return true. You can get or set the Width and Height properties on Size at any time.
true, false
Also You can use two arguments (width and height) or a Point (which stores width and height).
Tip In 2022 with .NET 7, the program compiles and runs correctly without any additional steps.
using System; using System.Drawing; Size size = new Size(); Console.WriteLine(size.IsEmpty); Console.WriteLine(size); Console.WriteLine(size.Width); Console.WriteLine(size.Height); size.Width = 1; Console.WriteLine(size); size = new Size(5, 10); Console.WriteLine(size.IsEmpty); Console.WriteLine(size); Point point = new Point(5, 10); size = new Size(point); Console.WriteLine(size.IsEmpty); Console.WriteLine(size); size = new Size(-1, -1); Console.WriteLine(size.IsEmpty); Console.WriteLine(size);
True {Width=0, Height=0} 0 0 {Width=1, Height=0} False {Width=5, Height=10} False {Width=5, Height=10} False {Width=-1, Height=-1}
Operators. The type implements addition and subtraction operators. Addition adds both widths and both heights. Subtraction takes the difference.
Note There also exists the Size.Add and Size.Subtract static methods. These are not shown in this program.
using System; using System.Drawing; Size a = new Size(5, 5); Size b = new Size(3, 2); Size c = a + b; Size d = a - b; Console.WriteLine(c); Console.WriteLine(d);
{Width=8, Height=7} {Width=2, Height=3}
Convert. SizeF stands for Size Float, and the Size type has several conversion methods available on it. You will need to choose between Ceiling, Truncate, and Round.
Note Ceiling raises to the next integer. Truncate removes everything past the decimal. Round rounds up or down.
using System; using System.Drawing; SizeF sizeF = new SizeF(4.5f, 5.6f); Size ceiling = Size.Ceiling(sizeF); Size truncate = Size.Truncate(sizeF); Size round = Size.Round(sizeF); Console.WriteLine(sizeF); Console.WriteLine(ceiling); Console.WriteLine(truncate); Console.WriteLine(round);
{Width=4.5, Height=5.6} {Width=5, Height=6} {Width=4, Height=5} {Width=4, Height=6}
SizeF. SizeF provides 2 floating point values: width and height. There are several constructors—with no parameters, you get an empty SizeF. You can also specify the width and height as a PointF.
using System; using System.Drawing; class Program { static void Main() { SizeF sizeF = new SizeF(); Display(sizeF); PointF pointF = new PointF(4.5f, 6.5f); sizeF = new SizeF(pointF); Display(sizeF); sizeF = new SizeF(sizeF); Display(sizeF); sizeF = new SizeF(6.5f, 10.5f); Display(sizeF); } static void Display(SizeF sizeF) { Console.WriteLine(sizeF); Console.WriteLine(sizeF.Width); Console.WriteLine(sizeF.Height); Console.WriteLine(sizeF.IsEmpty); } }
{Width=0, Height=0} 0 0 True {Width=4.5, Height=6.5} 4.5 6.5 False {Width=4.5, Height=6.5} 4.5 6.5 False {Width=6.5, Height=10.5} 6.5 10.5 False
Summary. The Size struct stores a width and a height in int fields. Size has methods that can simplify the logic in programs that handle size information.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 4, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.