无法修改“System.Collections.Generic.List.this[int]”的返回值
因为它不是变量
部分源码如下:
private List<RectangleEx> CRectLst = new List<RectangleEx>();
private List<Rectangle> RectLst = new List<Rectangle>();
其中RectangleEx为我自定义的类:
public class RectangleEx
{
private Rectangle rect;
public Rectangle Rect
{
get
{
return this.rect;
}
set
{
this.rect = value;
}
}
public int X
{
get
{
return this.rect.X;
}
set
{
this.rect.X = value;
}
}
public int Y
{
get
{
return this.rect.Y;
}
set
{
this.rect.Y = value;
}
}
public int Width
{
get
{
return this.rect.Width;
}
set
{
this.rect.Width = value;
}
}
public int Height
{
get
{
return this.rect.Height;
}
set
{
this.rect.Height = value;
}
}
public int Left
{
get
{
return this.rect.Left;
}
}
public int Right
{
get
{
return this.rect.Right;
}
}
public int Top
{
get
{
return this.rect.Top;
}
}
public int Bottom
{
get
{
return this.rect.Bottom;
}
}
public Point Location
{
get
{
return this.rect.Location;
}
set
{
this.rect.Location = value;
}
}
public RectangleEx(int x, int y, int width, int height)
{
this.rect = new Rectangle(x, y, width, height);
}
public bool Contains(int x, int y)
{
return this.rect.Contains(x, y);
}
public void Offset(int x, int y)
{
this.rect.Offset(x, y);
}
}
一个方法中这样这样写:
this.rect0 = new RectangleEx(50, 50, 40, 50);
this.RectLst.Add(this.rect0.Rect);
this.RectLst[0].X = 100; //报错,错误1
this.CRectLst.Add(this.rect0);
this.CRectLst[0].X = 1000;//未报错。
错误1:
无法修改“System.Collections.Generic.List<System.Drawing.Rectangle>.this[int]”的返回值,因为它不是变量。
而this.CRectLst[0].X = 1000;因为this.rect0为引用类型,就不报错。
为什么呢???
问题2:
而且在调用this.RectLst[0].Offset(100,100)的时候,不报错,但是this.RectLst[0]并没有被修改,难道是因为List集合中如果是值类型则会返回一个副本?返回的副本在调用Offset之后就被丢弃了,而原本的this.RectLst[0]没有被修改??
如果是引用类型,则返回一个对象的引用?
就像 String 对象的某些 方法 的 返回值那样??
对于错误1
this.RectLst[0].X 或者说
System.Collections.Generic.List<System.Drawing.Rectangle>.this[int] 返回的是什么呢??
相关推荐
浅谈Java中的this作为返回值时返回的是什么
Java中的this作为返回值时返回的是什么?下面小编就为大家介绍一下Java中的this作为返回值时返回。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Java Collections.sort()实现List排序的默认方法和自定义方法
主要介绍了Java Collections.sort()实现List排序的默认方法和自定义方法,需要的朋友可以参考下
c#遍历System.drawing.Color下面的所有颜色以及名称以查看
面试的时候被问到,如何遍历System.drawing.Color下面的所有颜色以及名称以查看,当时答得不好,现将方案记录如下: 代码如下:View Code public partial class Form1 : Form { FlowLayoutPanel newPanel = new FlowLayoutPanel(); public Form1() { InitializeComponent(); newPanel.AutoScroll = true;
Java Collections.sort()排序代码案例
主要介绍了Java Collections.sort()排序代码案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
将字符串转换成System.Drawing.Color类型的方法
将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下


非技术区
