如何在lambda表达式内传递ref参数? -线程问题 [英] How to pass a ref parameter inside lambda expression? - Thread issue

查看:176
本文介绍了如何在lambda表达式内传递ref参数? -线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要调用的方法.

I have a method to be called.

public void RecordConversation(ref ChannelResource cr)
{
    VoiceResource RecordResource = TServer.GetVoiceResource();
    RecordResource.MaximumTime = 6000;
    RecordResource.MaximumSilence = 6000;
    RecordResource.TerminationDigits = "";
}

要在线程中调用

Thread recordThread = new Thread(() => RecordConversation(ref ChanResource));
recordThread.Start();

我们当然会出错.

不能在匿名方法,lambda表达式或查询表达式中使用ref或out参数'ChanResource'

Cannot use ref or out parameter 'ChanResource' inside an anonymous method, lambda expression, or query expression

如何解决?

推荐答案

您不能使用ref.参见这个非常相同的问题.

话虽如此,您提到的原因(对象很大并且经常使用,我不想以值类型传递它)是无效的.

That being said, the reason you mentioned (The object is big and used often, I don't want to pass it in value type) is not valid.

如果删除ref ,则不会将参数作为值传递.在这里使用ref将不会有任何性能/内存增益. 参见值与参考之比.

You won't pass your parameter as value if you remove ref. There won't be any performance/memory gain to use ref here. See value vs reference.

这篇关于如何在lambda表达式内传递ref参数? -线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆