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发现并没有创建表的语句 未完待续 今天又遇到不创建表的问题 但是问题比较奇怪 ...
随机推荐
- ruby-----render讲解
Ruby rails页面跳转代码如下: 1.render(:text => string) 2.render(:inline => string, [:type => "r ...
- Linux下Maven私服Nexus3.x环境构建操作记录【转】
Maven介绍Apache Maven是一个创新的软件项目管理和综合工具.Maven提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文件.Ma ...
- [Java JNI] [Windows] [Visual Studio] [DLL] [UnsatisfiedLinkError]
有时候使用 GCC for Windows 生成 DLL 动态链接库时, 由于各种原因, 即使加载了动态链接库, JVM 仍然找不到符号, 从而产生 java.lang.UnsatisfiedLink ...
- [Windows] [DLL] [动态链接库技术]
cppblog.com 天空的博客 http://www.cppblog.com/API/archive/2011/03/10/141506.html cnblogs.com 吴秦的博客 http:/ ...
- NFine框架JqGrid导出选中行为Excel实现方法
客户端 function PostAndGetFileByUrl(url,type,postdata) { var temp; $.ajax({ url: url, type: type, data: ...
- 账号管理文件/etc/passwd和/etc/shadow
/etc/passwd和/etc/shadow是Linux中两个账号管理的重要文件 一./etc/passwd 这个文件中每一行代表一个账号,有几行就代表系统中存在几个账号.有些账号是系 ...
- 【LeetCode】89.Gary Code
Problem: The gray code is a binary numeral system where two successive values differ in only one bit ...
- java 通过域名获取ip
1. java InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com"); 2. nslookup
- 第23月第24天 git命令 .git-credentials git rm --cached git stash clear
在git push的时候,有时候我们会想办法撤销git commit的内容 1.找到之前提交的git commit的id git log 找到想要撤销的id 2.git reset –hard id ...
- Python 21 Django 实用小案例1
实用案例 验证码与验证 KindEditor 组合搜索的实现 单例模式 beautifulsoup4 验证码与验证 需要安装Pillow模块 pip stall pillow ...