列表与LT;>结构与属性。不能改变财产的价值。为什么? [英] List<> of struct with property. Cannot change value of property. why?

查看:59
本文介绍了列表与LT;>结构与属性。不能改变财产的价值。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这会返回以下错误:

"无法修改

的返回值''System.Collections.Generic.List< MyStruct> .this [int] ''因为它是

而不是变量'

我不明白为什么!列表是否返回其元素的副本?

为什么我不能更改元素本身?


类程序

{

private struct MyStruct

{

private int myVar;

public int MyProperty

{

get {return myVar; }

设置{myVar = value; }

}

}


private static List< MyStructlist = new List< MyStruct>();


private static void Main(string [] args)

{

MyStruct x = new MyStruct();

x.MyProperty = 45;

list.Add(x);

list [0] .MyProperty = 45; //< -----------错误在这里

}

}


Zytan

This returns the following error:
"Cannot modify the return value of
''System.Collections.Generic.List<MyStruct>.this[int]'' because it is
not a variable"
and I have no idea why! Do lists return copies of their elements?
Why can''t I change the element itself?

class Program
{
private struct MyStruct
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}

private static List<MyStructlist = new List<MyStruct>();

private static void Main(string[] args)
{
MyStruct x = new MyStruct();
x.MyProperty = 45;
list.Add(x);
list[0].MyProperty = 45; // <----------- ERROR HERE
}
}

Zytan

推荐答案

5月14日上午10点28分,Zytan< zytanlith ... @ gmail.comwrote:
On May 14, 10:28 am, Zytan <zytanlith...@gmail.comwrote:

这会返回以下错误:

"无法修改

的返回值''System.Collections.Generic.List< MyStruct> .this [int]''因为它是

而不是变量

我不明白为什么!列表是否返回其元素的副本?
This returns the following error:
"Cannot modify the return value of
''System.Collections.Generic.List<MyStruct>.this[int]'' because it is
not a variable"
and I have no idea why! Do lists return copies of their elements?



是的。事实上,列表上的[]运算符是一个函数,因此存储在列表中该位置的值

将作为函数结果返回,

on the堆栈。


这不会引起引用类型的问题,因为通常你想要改变引用类型的某些属性,所以事实是

你在列表中得到一份参考文件(不是实际的参考资料
列表中的
)并不会引起问题。


但是,对于值类型,完全相同的事情发生了,它确实会导致问题:将值从列表复制到堆栈中并且

作为函数结果返回。修改

课程的返回值对列表内容没有影响。明智的编译器

捕获了这个。

Yes. The [] operator on a list is, in fact, a function, so the value
stored at that location in the list is returned as a function result,
on the stack.

This doesn''t cause problems for reference types, because usually you
want to change some property of the reference type, so the fact that
you get a copy of the reference in the list (not the actual reference
that is in the list) doesn''t cause problems.

However, for value types exactly the same thing happens, and it does
cause problems: the value is copied from the list onto the stack and
returned as a function result. Modifying the returned value, of
course, has no effect on the contents of the list. The compiler wisely
catches this.


为什么我不能更改元素本身?


class Program

{

private struct MyStruct

{

private int myVar;

public int MyProperty

{

get {return myVar; }

设置{myVar = value; }

}

}


private static List< MyStructlist = new List< MyStruct>();


private static void Main(string [] args)

{

MyStruct x = new MyStruct();

x.MyProperty = 45;

list.Add(x);

list [0] .MyProperty = 45; //< -----------错误在这里

}

}
Why can''t I change the element itself?

class Program
{
private struct MyStruct
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}

private static List<MyStructlist = new List<MyStruct>();

private static void Main(string[] args)
{
MyStruct x = new MyStruct();
x.MyProperty = 45;
list.Add(x);
list[0].MyProperty = 45; // <----------- ERROR HERE
}
}



你需要这样做:


MyStruct y = list [0];

y.MyProperty = 45;

list [0] = y;

You need to do this:

MyStruct y = list[0];
y.MyProperty = 45;
list[0] = y;


是的。事实上,列表上的[]运算符是一个函数,因此存储在列表中该位置的值
Yes. The [] operator on a list is, in fact, a function, so the value

将作为函数结果返回,堆栈上的$>

stored at that location in the list is returned as a function result,
on the stack.



Ok。

Ok.


这不会引起参考类型的问题,因为通常你会/>
想要更改引用类型的某些属性,所以

你得到列表中引用的副本(不是实际引用)
$ b $列表中的b)不会引起问题。
This doesn''t cause problems for reference types, because usually you
want to change some property of the reference type, so the fact that
you get a copy of the reference in the list (not the actual reference
that is in the list) doesn''t cause problems.



对,这就是为什么它通常不是问题。

Right, so that''s why it''s normally not an issue.


但是,对于值类型完全相同的事情发生,它确实会导致问题:将值从列表复制到堆栈中并将

作为函数结果返回。修改

课程的返回值对列表内容没有影响。明智的编译器

抓住这个。
However, for value types exactly the same thing happens, and it does
cause problems: the value is copied from the list onto the stack and
returned as a function result. Modifying the returned value, of
course, has no effect on the contents of the list. The compiler wisely
catches this.



Yup,结构是值类型,所以这是有道理的。

Yup, and structs are value types, so this makes sense.


你需要这样做:


MyStruct y = list [0];

y.MyProperty = 45;

list [0] = y;
You need to do this:

MyStruct y = list[0];
y.MyProperty = 45;
list[0] = y;



好​​的,谢谢,布鲁斯!这很有帮助!


Zytan

Ok, thanks, Bruce!! This helps a lot!

Zytan


编译器明智地
The compiler wisely

抓住这个。
catches this.



注意,编译器不能捕获所有内容!以下代码

逃脱检测:


MyStruct x = new MyStruct();

list [index] .MethodThatChangesStructsFields(x );


我想这可以缩短为:


list [index] .MethodThatChangesStructsFields();

并且可能也会逃脱检测。该死的!我一直在改变

动态创建的所有ararys(长度未知)到

List<> ;,以避免Array.Resize。


Zytan

NOTE, the compiler does NOT catch everything! The following code
escapes detection:

MyStruct x = new MyStruct();
list[index].MethodThatChangesStructsFields(x);

I guess this could be shortened to:

list[index].MethodThatChangesStructsFields();

And likely THAT escapes detection, as well. Damn! I''ve been changing
all my ararys that are created dynamically (length unknown) into
List<>, to avoid Array.Resize.

Zytan


这篇关于列表与LT;&GT;结构与属性。不能改变财产的价值。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆