C#中的表达式...

编程入门 行业动态 更新时间:2024-10-15 10:17:13
本文介绍了C#中的表达式...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨! 当C#中的对象重载操作符时,我发现了一个非常有趣的行为: MyOjbects a,b,c,d; .... //在这里创建和初始化对象... 。 ... b.Length = 100; c.Length = 100; for(i = 0; i < 10000; i ++) { a = MyFun(2 * c)* b; } 在重叠的乘法运算符中,我检查: if(Op1.Length == Op2.Length)throw .... 大约100次迭代后异常触发...... !!? 不知何故,传递给MyFun的2 * c创建的临时变量 的内部对象是在临时变量第一次用于之前收集(无效)垃圾收集(无效)... 是否有一些已知的问题? 谢谢! Atmapuri

Hi! I noticed a very funny behaviour when overloading operators on objects in C#: MyOjbects a,b,c,d; .... //objects are created and initalized here... .... b.Length = 100; c.Length = 100; for (i = 0; i < 10000; i++) { a = MyFun(2*c)*b; } Inside the overladed multiplication operator, I check: if (Op1.Length == Op2.Length) throw.... And the exception triggers after about 100 iterations...!!? Somehow the internal objects of the temporary variable created by 2*c passed to MyFun are being garbage collected (nullified) before the temporary variable is used for the first time... Are there some known issues with this? Thanks! Atmapuri

推荐答案

你好 解决问题的方法是将表达式写成: d = 2 * c; a = MyFun (d)* b; 这是明确分配临时变量.... 当然,这不是一个真正的修复,它只需指向 问题... 谢谢! Atmapuri Atmapuri < di*@community.nospamwrote in message news:ej ************** @ TK2MSFTNGP04.phx.gbl ... Hi! The fix for the problem is to write the expression as: d = 2*c; a = MyFun(d)*b; That is to explicitely allocate the temporary variable.... But of course, that is not really a fix, it just points to the problem... Thanks! Atmapuri "Atmapuri" <di*@community.nospamwrote in message news:ej**************@TK2MSFTNGP04.phx.gbl... 嗨! 当C#中的对象重载运算符时,我注意到了一个非常有趣的行为: : MyOjbects a,b,c,d; ... //在这里创建和初始化对象... ... b.Length = 100; c.Length = 100; for( i = 0; i <10000; i ++) { a = MyFun(2 * c)* b; } 在重叠的乘法运算符中,我检查: if(Op1.Length == Op2.Length)throw .... 大约100次迭代后异常触发...... !!? 不知何故临时变量的内部对象 由2 *创建传递给MyFun的c是在第一次将临时变量用于 之前收集(无效)垃圾... 是有一些已知问题吗? 谢谢! Atmapuri Hi! I noticed a very funny behaviour when overloading operators on objects in C#: MyOjbects a,b,c,d; ... //objects are created and initalized here... ... b.Length = 100; c.Length = 100; for (i = 0; i < 10000; i++) { a = MyFun(2*c)*b; } Inside the overladed multiplication operator, I check: if (Op1.Length == Op2.Length) throw.... And the exception triggers after about 100 iterations...!!? Somehow the internal objects of the temporary variable created by 2*c passed to MyFun are being garbage collected (nullified) before the temporary variable is used for the first time... Are there some known issues with this? Thanks! Atmapuri

听起来好奇;你可以用简单的(但是完整的)可以重现这个吗? 代码? MyFun是否有可能返回一个MyOjbects [原文如此]实例 真的与b(100)的长度相同吗?同样,取决于这些结构或类是否为,可能是您(在未显示的 是副作用吗? Marc Sounds curious; can you reproduce this in simple (yet complete), postable code? Is it possible that MyFun is return an MyOjbects [sic] instance that genuinely has the same length as b (100)? Equally, depending on whether these are structs or classes, it might be that you are (in the non-shown code) accidentally changing properties of the operands, and this behaviour is a side-effect? Marc

这实际上是非常的,非常错误。还有其他事情发生在这里。由于调用2 * c而创建的 临时变量不是因为引用保存在堆栈上(通过参数) 当你把它传递给MyFun时。 绝对是别的东西。如果没有看到运算符过载的 类的代码,以及MyFun的代码,那就不用说了。 无法说出来。 - - Nicholas Paldino [.NET / C#MVP] - mv*@spam.guard.caspershouse " Atmapuri" < di*@community.nospamwrote in message news:ej ************** @ TK2MSFTNGP04.phx.gbl ... That''s actually very, very wrong. Something else is going on here. The temporary variable that is created as a result of calling 2 * c isn''t being GC''ed because the reference is held on the stack (through the parameter) when you pass it to MyFun. It is most definitely something else. Without seeing the code for the class where the operator is overloaded, as well as the code for MyFun, it''s impossible to say. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse "Atmapuri" <di*@community.nospamwrote in message news:ej**************@TK2MSFTNGP04.phx.gbl... 嗨! 当C#中的对象重载运算符时,我注意到了一个非常有趣的行为: : MyOjbects a,b,c,d; ... //在这里创建和初始化对象... ... b.Length = 100; c.Length = 100; for( i = 0; i <10000; i ++) { a = MyFun(2 * c)* b; } 在重叠的乘法运算符中,我检查: if(Op1.Length == Op2.Length)throw .... 大约100次迭代后异常触发...... !!? 不知何故临时变量的内部对象 由2 *创建传递给MyFun的c是在第一次将临时变量用于 之前收集(无效)垃圾... 是有一些已知问题吗? 谢谢! Atmapuri Hi! I noticed a very funny behaviour when overloading operators on objects in C#: MyOjbects a,b,c,d; ... //objects are created and initalized here... ... b.Length = 100; c.Length = 100; for (i = 0; i < 10000; i++) { a = MyFun(2*c)*b; } Inside the overladed multiplication operator, I check: if (Op1.Length == Op2.Length) throw.... And the exception triggers after about 100 iterations...!!? Somehow the internal objects of the temporary variable created by 2*c passed to MyFun are being garbage collected (nullified) before the temporary variable is used for the first time... Are there some known issues with this? Thanks! Atmapuri

更多推荐

C#中的表达式...

本文发布于:2023-11-06 08:09:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!