这个返回如下错误:"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. Kafka权威指南 读书笔记之(五)深入Kafka

    集中讨论以下3 个有意思的话题 :• Kafka 如何进行复制:• Kafka 如何处理来自生产者和消费者的请求 :• Kafka 的存储细节,比如文件格式和索引. 集群成员关系 Kafka 使用 Z ...

  2. 【英文文档】 Installing Go from source Go语言官方编译指南 2019.02.27

    Introduction Go is an open source project, distributed under a BSD-style license. This document expl ...

  3. canvas高级篇(转载)移动元素

    本文转载在http://bbs.blueidea.com/thread-2979405-1-1.html 哈哈哈,好骚气!终于解决了我的需求.可以移动canvas内的多个元素 <!DOCTYPE ...

  4. 细说shiro之二:组件架构

    官网:https://shiro.apache.org/ Shiro主要组件包括:Subject,SecurityManager,Authenticator,Authorizer,SessionMan ...

  5. Quartz.NET 配置文件详解

    Ø  前言 在之前的 使用 Topshelf 结合 Quartz.NET 创建 Windows 服务 文章中,使用到了 Quartz 的两个配置文件 quartz.config 和 quartz_jo ...

  6. 搭建阿里云lnmp环境 (centos7+nginx+MySQL5.7.9+PHP7)

    阿里云一台服务器出现问题! 我估计是一键安装包环境的原因,所以打算重新搭建下环境! 首先,当然是先做好快照!安全第一! 对系统盘做更换系统操作,装上纯净版的centos. 装好后,进入系统 一.挂载数 ...

  7. PHP实现流程管理功能

    核心逻辑:流程管理,在各种系统中扮演很重要的地位,可以把设定好的流程放入系统中,规定好几个节点,只要所有节点都通过,就可以通过. 建立四张数据库表: 1.我们首先做一个新建流程页面 flow.php, ...

  8. tensorflow/core/kernels/conv_ops.cc:659] check failed: stream->parent()->getc

    tensorflow版本与cudnn版本不对应,如tensorflow-gpu==1.1.0,cudnn==6.0 升级tensorflow-gpu到更高版本

  9. MSSQL 临时表和公用表使用案例

    1.临时表: 1.1)实例1 if(OBJECT_ID('tempdb..#a') IS NOT NULL) drop table #a; if(OBJECT_ID('tempdb..#b') IS ...

  10. C#后台画图保存为ipg/png的文件

    public void Exec1()        { string imgurl = @"http://api.senwoo.com/Content/HeadPortrait/" ...