为了账号安全,请及时绑定邮箱和手机立即绑定

C#在foreach循环中使用点的偏移量方法来修改点的数组

C#在foreach循环中使用点的偏移量方法来修改点的数组

C#
一只萌萌小番薯 2021-05-05 17:21:32
我在理解点的偏移量方法在foreach循环中如何修改现有点数组时遇到麻烦。我可以通过手动索引每个数组实体来做到这一点,但是我强烈怀疑那不是应该怎么做。*编辑要清楚。将偏移点存储在MyPoints数组中的最佳方法是什么?请参见下面的代码。我使用http://rextester.com/在线C#编译器来测试代码。using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;using System.Drawing;namespace Rextester{    public class Program    {        static int ii = 0;        public static void Main(string[] args)        {            Point[] MyPoints =             {                new Point(0,0),                new Point(10,10),                new Point(20,0)            };            foreach(Point p in MyPoints)            {                p.Offset(5,2); //this works but does not store the new points                             }                  //in MyPoints array when the foreach loop                                //exits, which is what I want.        Console.WriteLine("p1x = {0} \t p1y = {1}", MyPoints[0].X, MyPoints[0].Y);        Console.WriteLine("p2x = {0} \t p2y = {1}", MyPoints[1].X, MyPoints[1].Y);        Console.WriteLine("p3x = {0} \t p3y = {1} \n", MyPoints[2].X, MyPoints[2].Y);        foreach(Point p in MyPoints)        {            MyPoints[ii].Offset(5,2);            ii++;        }        Console.WriteLine("p1x = {0} \t p1y = {1}", MyPoints[0].X, MyPoints[0].Y);        Console.WriteLine("p2x = {0} \t p2y = {1}", MyPoints[1].X, MyPoints[1].Y);        Console.WriteLine("p3x = {0} \t p3y = {1}", MyPoints[2].X, MyPoints[2].Y);        }    }}//This yields the following/*p1x = 0      p1y = 0p2x = 10     p2y = 10p3x = 20     p3y = 0 p1x = 5      p1y = 2p2x = 15     p2y = 12p3x = 25     p3y = 2*/
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 200 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信