报错信息
Error CS1628 C#不能在匿名方法、lambda表达式、查询表达式或本地函数中使用ref、Out或in参数
Error CS1628 Cannot use ref, out, or in parameter ‘xx’ inside an anonymous method, lambda expression, query expression, or local function
问题描述
下面问题会报这个错误。
static void test(out System.Drawing.Image[] bitmaps)
{
int count = 10;
bitmaps = new System.Drawing.Image[count];
Parallel.For(0, count, (i) =>
{
bitmaps[i] = System.Drawing.Image.FromFile("2.bmp");
});
}
解决方案
The code below will throw out this error. Because the change brought by the lambda expression (i)=>{...}
only works in the function test
.
因为lambda表示式(i)=>{...}
的作用域是test函数内。所以,它不能去修改test函数的带out,in,ref的参数
如果你直接将test函数里的out关键词去掉,就不会报错了。
但是,我就是要用out,ref,in关键词怎么办呢? 我需要用这个函数来传出运算结果啊
加个临时变量来过渡一下,就好了。 代码如下
static void test(out System.Drawing.Image[] bitmaps)
{
int count = 10;
System.Drawing.Image[] bitmapsTemp = new System.Drawing.Image[count];
Parallel.For(0, count, (i) =>
{
bitmapsTemp[i] = System.Drawing.Image.FromFile("2.bmp");
});
bitmaps = bitmapsTemp;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)