Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why there is no Center() method for Rectangle class in c#?

Tags:

c#

previously, there is such method for Rectangle in MFC, i dont know why there is not for the c# version.

like image 842
Benny Avatar asked Sep 09 '09 08:09

Benny


2 Answers

Presumably it wasn't deemed useful enough to merit inclusion.

You could easily add it as an extension method if you want though (and if you're using C# 3):

public static Point Center(this Rectangle rect)
{
    return new Point(rect.Left + rect.Width/2,
                     rect.Top + rect.Height / 2);
}

Note that as the values are expressed as integers, you could easily end up getting a non-exact value, assuming you want to return a Point rather than another structure using decimal or double.

The above is actually for the System.Drawing.Rectangle struct. If you're talking about a different Rectangle, please add the appropriate information and I'll edit my answer.

like image 174
Jon Skeet Avatar answered Nov 12 '22 08:11

Jon Skeet


It exists within the (no longer supported) Microsoft.XNA.Framework.

Rectangle.Center Property

myRectangle.Center returns a Point

like image 42
Evorlor Avatar answered Nov 12 '22 10:11

Evorlor