这个返回如下错误:"Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this[int]' because it is not a variable"

class Program
{
private struct MyStruct
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
} private static List<MyStructlist> list = new List<MyStruct>(); private static void Main(string[] args)
{
MyStruct x = new MyStruct();
x.MyProperty = ;
list.Add(x);
list[].MyProperty = ; // <----------- ERROR HERE
}
}

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.

//You need to do this:

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

List<> of struct with property. Cannot change value of property. why?的更多相关文章

  1. error MSB8031: Building an MFC project for a non-Unicode character set is deprecated. You must change the project property to Unicode or download an additional library. See http://go.microsoft.co

    Win10,也重新装了免费版的Visual Studio 2013 y,写MFC程序时候发现这样的提示: error MSB8031: Building an MFC project for a no ...

  2. hibernate配置之<property name="hbm2ddl.auto">create</property>导致每次创建SessionFactory都清空数据库中的数据

    参考:http://stackoverflow.com/questions/6611437/how-to-make-hibernate-not-drop-tables 我遇到的问题就是: List l ...

  3. Xcode升级了6.3 出现的警告:Auto property synthesis will not synthesize property

    1.  Auto property synthesis will not synthesize property 'title'; it will be implemented by its supe ...

  4. Change An Item Property Using Set_Item_Property In Oracle Forms

    Set_Item_Property is used to change an object's settings at run time. Note that in some cases you ca ...

  5. FCC---Use the CSS Transform scale Property to Change the Size of an Element

    To change the scale of an element, CSS has the transform property, along with its scale() function. ...

  6. iOS-Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its super

    今天在XCode6.3上面重写TabBar的时候,自定义tabBar的代理遇到的一个问题 在重写tabBar的代理的时候遇到了一个警告. 解决方法: 在.m文件中 警告消失.

  7. Xcode warning:Auto property synthesis will not synthesize property

    iOS 警告提示如下: 添加 @dynamic告诉编译器这个属性是动态的,动态的意思是等你编译的时候就知道了它只在本类合成; 如下:

  8. Auto property synthesis will not synthesize property &#39;delegate&#39;; it will be implemented by its super

    今天在XCode6.3上面重写TabBar的时候,自己定义tabBar的代理遇到的一个问题 在重写tabBar的代理的时候遇到了一个警告. 解决方法: 在.m文件里 警告消失

  9. mysql不创建表 <property name="hbm2ddl.auto">update</property> 无效

    netbeans win10 mysql8 hibernate 4.3.11 dakai mysql的general_log发现并没有创建表的语句 未完待续 今天又遇到不创建表的问题 但是问题比较奇怪 ...

随机推荐

  1. springMVC中a标签传递多个参数到后台的应用

    1.js页面:返回json填充HTML部分代码 <a class='byCard' href="+path+"/static/toCardView/"+ data. ...

  2. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  3. JVM简析

    JVM结构图 1.程序计数器 是最小的一块内存区域,它的作用是当前线程所执行的字节码的行号指示器,在虚拟机的模型里,字节码解释器工作时就是通过改变这个计数器的值来选取下一条需要执行的字节码指令,分支. ...

  4. 他山之石--机器学习 step by step

    练习使用的数据 diabetes.csv 备用百度网盘地址 输入变量与输出变量均为连续变量的预测问题是回归问题: 输出变量为有限个离散变量的预测问题成为分类问题: 其实回归问题和分类问题的本质一样,都 ...

  5. Eclipse使用Maven搭建Java Web项目,并直接部署Tomcat

    1.环境: win10 Java 1.8 Maven 3.3.9 Eclipse IDE for Java EE Developers 2.准备: eclipse环境什么的不赘述,Maven环境还是要 ...

  6. oracle杀掉连接

    相关sql --查看当前连接 select count(*) from v$process --数据库允许的最大连接数 select value from v$parameter where name ...

  7. JS中var、let、const区别? 用3句话概括

    使用var声明的变量,其作用域为该语句所在的函数内,且存在变量提升现象: 使用let声明的变量,其作用域为该语句所在的代码块内,不存在变量提升: 使用const声明的是常量,在后面出现的代码中不能再修 ...

  8. Extjs.net Button点击下载jpg图片

    <ext:Button ID=" AutoPostBack="false"> <DirectEvents> <Click OnEvent=& ...

  9. Win10 64位连接LJM1005打印机局域网访问

    除了网上常见的开Guest用户之类需要额外三个设置 (1)安装LJM1005驱动LJM1005_Full_Solution (2)设置打印机共享和安全中的everyone全部勾选(解决能看到打印机无法 ...

  10. select实现简单TCP通信(ubuntu 18.04)

    一.服务器程序(server.c) #include <stdio.h> #include <unistd.h> #include <stdlib.h> #incl ...