这个返回如下错误:"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. PHP中的表单传值

     前言:试想一下如果PHP没有了表单传值,那么就相当于一个没有表达能力的人,因此表单传值在php中是相当重要的,但是不难,接下来我们一起来探讨一下吧------> (一)什么是表单传值 概念:表 ...

  2. 使用 highlight.js 在网页中高亮显示java 代码 【原】

    <html> <head> <meta charset="UTF-8"> <script src="http://apps.bd ...

  3. Java基础高级部分(一)

    1. 集合部分 1.1 HashMap排序 package cn.Douzi.hashMap01; import java.util.ArrayList; import java.util.Colle ...

  4. Centos7安装官方JDK

    一.下载jdk最新版本版本 链接地址:官方地址 二.上传jdk到centos下 三.检查当前linux系统上是否有jdk,linux命令:rpm -qa | grep java 查询结果: 卸载掉系统 ...

  5. scoketserver模块(TCP协议 与 udp 协议)

    socketserver模块 ( 基于 TCP 协议 ) import socketserver # 自定义类用来处理通信循环 class MyTCPhanler(socketserver.BaseR ...

  6. final 关键字:用来修饰类,方法,成员变量,局部变量

    final 关键字:用来修饰类,方法,成员变量,局部变量 表示最终的不可变的 1.final修饰一个类 表示当前的类不能有子类,也就是不能将一个类作为父类 格式: public final class ...

  7. Android几个比较有用的插件

    1.Android  Drawable Importer 2.Android ButterKnife Zelezny 使用方法,在SetContentView上右键,Generate 3.Androi ...

  8. Python 生成requirement 使用requirements.txt安装类库

    快速生成requirement.txt的安装文件 (CenterDesigner) xinghe@xinghe:~/PycharmProjects/CenterDesigner$ pip freeze ...

  9. IT这条路,适合什么人走。

    今天 ,到图书馆Study,呼,不知道为撒,看到那么多新书,那么多新技术(也不能说是新技术,就是自己没有学习过的技术),特别兴奋,学习的疲劳顿时间就没了,感觉什么都想学,都想据为己有,但是...... ...

  10. 将.csv数据导入到mysql中

    1.首先看一下我需要导入的数据: 用excel打开的时候显示: 用notepad++打开显示为: 2.使用notepad++打开改变字符集为UTF-8 3,建立表,表中的字段要和文件中的一致 3.执行 ...