'ByRef' parameter '<parametername>' cannot be used in a lambda expression

元气小坏坏 提交于 2019-11-29 16:25:38

I know that question is 4 years old but i'm just facing the same problem and i figured it out so i want to share the solution with you.

According to the Microsoft answer on the MSDN page:

You have to assign the ByRef parameter to a local variable, and use the local variable in the lambda expression.

Hope the answer help anyone.

You can't declare the Sub delegate with ByRef parameters (ref or out in C#), no matter if you using an anonymous function or not.

But you can declare your delegate type and then use it even with your anonymous function

On MSDN it mentions the following rules apply to variable scope in lambda expressions:

  • A variable that is captured will not be garbage-collected until the delegate that references it goes out of scope.
  • Variables introduced within a lambda expression are not visible in the outer method.
  • A lambda expression cannot directly capture a ref [ByRef in VB] or out parameter from an enclosing method.
  • A return statement in a lambda expression does not cause the enclosing method to return.
  • A lambda expression cannot contain a goto statement, break statement, or continue statement whose target is outside the body or in the body of a contained anonymous function.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!