tags:

views:

560

answers:

1

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

+12  A: 

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.

Jon Skeet