我在理解點(diǎn)的偏移量方法在foreach循環(huán)中如何修改現(xiàn)有點(diǎn)數(shù)組時(shí)遇到麻煩。我可以通過手動(dòng)索引每個(gè)數(shù)組實(shí)體來做到這一點(diǎn),但是我強(qiáng)烈懷疑那不是應(yīng)該怎么做。*編輯要清楚。將偏移點(diǎn)存儲(chǔ)在MyPoints數(shù)組中的最佳方法是什么?請(qǐng)參見下面的代碼。我使用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 回答
- 0 關(guān)注
- 242 瀏覽
添加回答
舉報(bào)
0/150
提交
取消