C# 如何在c中打印出矩形值?

C# 如何在c中打印出矩形值?,c#,C#,我想检查矩形坐标是否正确存储在数组中。但是我不知道如何打印值。您必须访问要显示的属性: foreach(System.Drawing.Rectangle rect in _rectangleList) { MessageBox.Show(rect); } 也可以在填充数组时简单地设置断点,并使用调试器检查其内容。如果您使用的是Visual Studio,则可以将插入符号放置在阵列上,然后按Shift-F9查看内容,或从菜单中选择“快速观察”。在MessageBox中打印坐标: fo

我想检查矩形坐标是否正确存储在数组中。但是我不知道如何打印值。

您必须访问要显示的属性:

 foreach(System.Drawing.Rectangle rect in _rectangleList)
 {
   MessageBox.Show(rect);
 }

也可以在填充数组时简单地设置断点,并使用调试器检查其内容。如果您使用的是Visual Studio,则可以将插入符号放置在阵列上,然后按Shift-F9查看内容,或从菜单中选择“快速观察”。

在MessageBox中打印坐标:

foreach(System.Drawing.Rectangle rect in _rectangleList)
{
    MessageBox.Show($"X:{rect.X} Y:{rect.Y} Width:{rect.Width} Height:{rect.Height}");
}

对于矩形的高度,你必须说
矩形高度

那么这有什么用呢?你试过阅读和研究你得到的结果吗?是的,但是如果OP需要不同的值,比如rect.Left或rect.Right,他可以相应地改变他的输出。你不必这样做。
MessageBox.Show("X:" + rect.Location.X + " Y:" + rect.Location.Y);
You can create your own extension method on the rectangle that will print out all the details that you . 

Check my example here.

 public static class RectangleExtensions
    {
        public static string Details(this System.Drawing.Rectangle rectangle)
        {
            var details = new StringBuilder();
            details.AppendFormat("Height: {0}", rectangle.Height);
            details.AppendFormat("Bottom: {0}", rectangle.Bottom);
            details.AppendFormat("Left: {0}", rectangle.Left);
            details.AppendFormat("Right: {0}", rectangle.Right);

            return details.ToString();
        }
    }
    class Program
    {


        static void Main(string[] args)
        {
            var rectangle = new System.Drawing.Rectangle();


            Console.WriteLine(rectangle.Details());
        }
    }