怎么直接修改List里面元素的数值?

marklr 2014-07-30 10:39:18

List<System.Drawing.Point> testList = new List<System.Drawing.Point>();

System.Drawing.Point a= new System.Drawing.Point(1, 2);
System.Drawing.Point b = new System.Drawing.Point(3, 4);

testList.Add(a);
testList.Add(b);

//如果现在我想修改testList里面第一个元素的Y数值+1,该怎么修改,我用的方法是

testList[0] = new System.Drawing.Point(testList[0].X, testList[0].Y + 1);

//但我觉得这样太麻烦了,怎么才可以直接修改里面的Y数值?
...全文
33330 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
lc_ant 2014-07-31
  • 打赏
  • 举报
回复
b.Y = b.Y + 1;
testList[i] = b;
  • 打赏
  • 举报
回复
List<对象> 别名; foretch(){} for(){} 第一种 直接点出你要的属性赋值即可 第二种 主要靠下标吃饭 [第几个]点出你的属性赋值即可
twtiqfn 2014-07-31
  • 打赏
  • 举报
回复
在控件里直接改不行吗
smthgdin_020 2014-07-30
  • 打赏
  • 举报
回复
引用 6 楼 marklr 的回复:
那谁知道有什么简洁的写法?
可以参考12,13楼。
phommy 2014-07-30
  • 打赏
  • 举报
回复
引用 16 楼 gomoku 的回复:
[quote=引用 14 楼 phommy 的回复:] var arr = l.GetType().GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(l) as Point[]; ...
这样做更不可取。因为 1、性能及其低下 2、可能兼容不了未来的版本(DotNet可以改变List的内部实现)。[/quote] 性能没有你说的那么夸张,除非成千上万次执行(那样的话可以通过用il改写解决,嫌麻烦的话也可以仅缓存这个数组即可) 至于担心List的内部实现变化,那就是杞人忧天了,先不说可能性,就算真改了,这种.net版本级的升级你想让你的代码不修改而只是重编译下就直接跑本身就是奢望 当然,实际中确实不大可能这样,不过这大概是直接解答楼主问题(而不是反问“你为什么要这么做”或“你为什么不这么做”)的最佳方案
gomoku 2014-07-30
  • 打赏
  • 举报
回复
引用 14 楼 phommy 的回复:
var arr = l.GetType().GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(l) as Point[]; ...
这样做更不可取。因为 1、性能及其低下 2、可能兼容不了未来的版本(DotNet可以改变List的内部实现)。
gomoku 2014-07-30
  • 打赏
  • 举报
回复
引用 12 楼 diaodiaop 的回复:
[code=csharp] public class point { public int x{get;set;} public int y{get;set;} } ...
一般来说,为了简化写法,把简单类型做成类不好,因为每个类成员有8个字节的额外负担。 GDI+要用到Point的时候,可能还得转回去。
phommy 2014-07-30
  • 打赏
  • 举报
回复
var l = new List<Point> {new Point(100, 100), new Point(110, 110)}; var arr = l.GetType().GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(l) as Point[]; arr[0].X = 200; arr[1].Y = 300; Console.WriteLine(l[0].ToString()); //{X=200,Y=100}
於黾 2014-07-30
  • 打赏
  • 举报
回复
那就别定义List<System.Drawing.Point> 而定义成List<int,int> 就可以随便改了
by_封爱 版主 2014-07-30
  • 打赏
  • 举报
回复

public class point
{
public int x{get;set;}
public int y{get;set;}
}
public list<System.Drawing.Point> GetPointList(List<point> list)
{
   return list.select(d=>new System.Drawing.Point(d.x, d.y));
}

void main(string args)
{
var list=new list<point>();
list.add(new point(1,2));
list.add(new point(1,2));
list[0].x=123123123;

var xxoo=GetPointList(list);
}
我觉得这样挺好的..没必要泛型中非得是系统的东西 自定义也很不错呀..
by_封爱 版主 2014-07-30
  • 打赏
  • 举报
回复
自己定义对象 x y 然后写个获取的方法就行了啊...
marklr 2014-07-30
  • 打赏
  • 举报
回复
除了自定义方法之外没有其他方案了?
於黾 2014-07-30
  • 打赏
  • 举报
回复
void changePoint(ref point p,int x,int y) { p=new point(p.x+x,p.y+y); }
於黾 2014-07-30
  • 打赏
  • 举报
回复
想简洁,首先,using System.Drawing,你就不用写System.Drawing.了 其次,把修改坐标的部分封装成方法,直接调用
於黾 2014-07-30
  • 打赏
  • 举报
回复
testList[0]=new point(0,0) point不能单独修改x和y,必须整体赋值
marklr 2014-07-30
  • 打赏
  • 举报
回复
那谁知道有什么简洁的写法?
marklr 2014-07-30
  • 打赏
  • 举报
回复
引用 1 楼 hanhualangzi 的回复:
testList[0].X=100;
无法修改“System.Collections.Generic.List<System.Drawing.Point>.this[int]”的返回值,因为它不是变量
exception92 2014-07-30
  • 打赏
  • 举报
回复
Point 类是结构类型,etestList[0].Y=4 这样赋值也不对的。
Landy_cc 2014-07-30
  • 打赏
  • 举报
回复
testList[0].Y + =1;
gomoku 2014-07-30
  • 打赏
  • 举报
回复
Point是结构体,操作List元素一定要整体赋值(List[i]得到的是一个拷贝)。
加载更多回复(5)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧