C#问题,错误地方该怎么改,请教别人问题的邮件

本帖子已过去太久远了,不再提供回复功能。3245人阅读
Unity(8)
题目中问题是我批量修改字典的数据时遇到的问题,作为小白真是发现好多东西没有接触到啊。
上错误代码
& & & & //foreach (KeyValuePair&string,float& item in dataDic)
& & & & //{
& & & & & &
& & & & // & &for (int i = 0; i & cars.L i++)
& & & & // & &{
& & & & // & & & &if (cars[i].name.Contains(item.Key))
& & & & // & & & &{
& & & & // & & & & & &dataDic[item.Value.ToString()] = cardatas[i].currentS
& & & & // & & & & & &
& & & & // & & & &}
& & & & // & &} & & & & &
& & & & //}
InvalidOperationException: out of sync
System.Collections.Generic.Dictionary`2+Enumerator[System.String,System.Single].VerifyState () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:912)
System.Collections.Generic.Dictionary`2+Enumerator[System.String,System.Single].MoveNext () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:835)
RaceData.Updatedatas () (at Assets/Scripts/Data/RaceData.cs:164)
RaceData.Update () (at Assets/Scripts/Data/RaceData.cs:80)
网上一顿搜集资料,说是不能用迭代去修改Dictionary,需要先把字典作为一个Buffer引出来,然后修改Buffer
& & & & List&string& buffer = new List&string&(dataDic.Keys);
& & & & foreach (string item in buffer)
& & & & & & for (int i = 0; i & cars.L i++)
& & & & & & {
& & & & & & & & if (cars[i].name.Contains(item))
& & & & & & & & {
& & & & & & & & & & dataDic[item.ToString()] = cardatas[i].currentS
& & & & & & & & & &
& & & & & & & & }
& & & & & & }
//这样就好使了,当然还有其他方法修改
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:8376次
排名:千里之外
原创:10篇
http://www.skywind.me/blog/ http://www.tracefact.net/
(7)(1)(1)(1)(2)(2)[C#] struct错误:无法修改“…”的返回值,因为它不是变量 | 小谢的小站
TearSnow Fan
我一句话都不说……
您当前位置 :
>> [C#] struct错误:无法修改“…”的返回值,因为它不是变量
[C#] struct错误:无法修改“…”的返回值,因为它不是变量
今天师妹调试程序时,出现了编译错误 CS1612
Cannot modify the return value of ‘expression‘ because it is not a variable
无法修改“expression”的返回值,因为它不是变量
出现这种错误的最常见情况是使用了如下的赋值语句:
AnObject.AnStruct.Vaule =
以如下的程序为例,初一看似乎没什么问题,实际上根本无法编译。
public struct Rectangle
private int _W
private int _H
public int Width
get { return _W }
set { _Width = }
public int Height
get { return _H }
set { _Height = }
public Rectangle(int width, int height)
this._Width =
this._Height =
public class Draw
private Rectangle _rect = new Rectangle(0, 0);
public Rectangle Rect
get { return _ }
set { _rect = }
public Draw() { }
public Draw(int mapWidth, int mapHeight)
_rect = new Rectangle(mapWidth, mapHeight);
class Program
static void Main(string[] args)
Draw draw = new Draw();
draw.MyRect.Width = 20;
//《== 编译错误
draw.MyRect.Height = 12;
//《== 编译错误
draw是引用类型,MyRect是struct,也就是值类型,当访问draw.MyRect时,会首先调用如下的程序:
public Rectangle MyRect
get { return _ }
set { _Width = } //只用到了get,和set无关
因为值类型默认是按值传递的,因此返回的是栈上的 _rect 的一份临时的、本地的拷贝,我们暂且称之为 temp_rect 好了。
因此,使 draw.MyRect.Width = 20;就等同于使这个 temp_rect.Width = 20;
由于存在栈上的这个本地副本,即使能对它进行修改,也无法最终反映到 draw.MyRect 本身(即 _rect)上,因此这是一个毫无何意义的操作,故C#编译器就从源头上禁止了这样的操作。
解决方法:
把struct替换成class,这样,MyRect就成了一个对象(引用类型),draw.MyRect就会返回_rect在堆上的实际地址,修改也会反映到实际的对象
如果非要用struct不可的话,需重新生成一个所用到的struct,即设置一个中间变量:
Draw draw = new Draw();
Rectangle tempRect = new Rectangle();
tempRect.Width = 20;
tempRect.Height = 12;
draw.MyRect = tempR
这样实际上是替换 draw 对象的整个 _rect 属性(即整个struct)。
参考资料:
[1] CSDN博客 - 站在巨人的肩膀上:
本文固定链接:
【上一篇】【下一篇】
您可能还会对这些文章感兴趣!
最新日志热评日志随机日志
官方微信扫一扫
日志总数:151 篇
评论总数:406 篇
分类总数:9 个
标签数量:251 个
链接总数:10 个
建站日期:
运行天数:1698 天
最后更新:
如果您觉得本站的内容对您有帮助,非要感谢一下不可,那么请给小谢捐赠。}

我要回帖

更多关于 请教一个问题 英文 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信