List<> of struct with property. Cannot change value of property. why?
这个返回如下错误:"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?的更多相关文章
- 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 ...
- hibernate配置之<property name="hbm2ddl.auto">create</property>导致每次创建SessionFactory都清空数据库中的数据
参考:http://stackoverflow.com/questions/6611437/how-to-make-hibernate-not-drop-tables 我遇到的问题就是: List l ...
- 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 ...
- 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 ...
- 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. ...
- iOS-Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its super
今天在XCode6.3上面重写TabBar的时候,自定义tabBar的代理遇到的一个问题 在重写tabBar的代理的时候遇到了一个警告. 解决方法: 在.m文件中 警告消失.
- Xcode warning:Auto property synthesis will not synthesize property
iOS 警告提示如下: 添加 @dynamic告诉编译器这个属性是动态的,动态的意思是等你编译的时候就知道了它只在本类合成; 如下:
- Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its super
今天在XCode6.3上面重写TabBar的时候,自己定义tabBar的代理遇到的一个问题 在重写tabBar的代理的时候遇到了一个警告. 解决方法: 在.m文件里 警告消失
- mysql不创建表 <property name="hbm2ddl.auto">update</property> 无效
netbeans win10 mysql8 hibernate 4.3.11 dakai mysql的general_log发现并没有创建表的语句 未完待续 今天又遇到不创建表的问题 但是问题比较奇怪 ...
随机推荐
- python爬虫慕课基础1
test_urllib2.py import http.cookiejar from urllib import request url = "http://www.baidu.com&qu ...
- vue常用指令
1.v-if系列 v-if="数据|判断" 只要条件成立,就显示if中的元素 v-else (注意:必须跟在v-if或者v-if-else的后面,不然失效) 如果if条件不成立显示 ...
- mvn项目压缩打包
通常情况下,maven打包结果为jar或war包.如果需要一并打包配置文件等参数,通过resources配置指定需要打包的文件参数,如下示例: <project> ... <!-- ...
- docker搭建lnmp环境
1.搭建lnmp网站平台 1.创建mysql数据库容器 docker run -it -d --name lnmp_mysql -p 3308:3306 -e MYSQL_ROOT_PASSWORD= ...
- Slash and BackSlash 记忆法
容易混淆的两兄弟 Slash https://en.wikipedia.org/wiki/Slash Slash (punctuation), the "/" punctuatio ...
- c# c/s 框架读取的配置文件时是app.exe.config
c# c/s 框架读取的配置文件时是app.exe.config, 一般在bin中间中俄debug中或者Release中
- Important persons in deep learning
Geoffrey Hinton Pieter Abbeel Ian Goodfellow, GAN之父 2019.3 任 Apple Director Andrej Karpathy. 视觉大牛,李飞 ...
- luogu P3240 [HNOI2015]实验比较
传送门 首先根据题目条件,题目中如果是=的点可以缩起来,然后\(a<b\)连边\(a\rightarrow b\),而且所有点入度为最多1,那么判掉有环的不合法情况,题目中的依赖关系就是一颗外向 ...
- 实现两线程的同步二(lockSupport的park/unpark)
1.使用LockSupport的part/unpark实现 package com.ares.thread; import java.util.concurrent.locks.LockSupport ...
- Lr-代理录制
哈哈,第一讲,决定分享一下代理的一些知识,是我学习的总结,可能会有错误,欢迎大家指正. 问题一:代理录制是为了解决什么问题或者说为什么么要使用代理呢? 这是因为lr只能使用IE浏览器进行录制,如果想使 ...