C# Cannot use ref or out parameter inside an anonymous method body

后端 未结 3 1826
庸人自扰
庸人自扰 2020-12-09 07:40

I\'m trying to create a function that can create an Action that increments whatever integer is passed in. However my first attempt is giving me an error \"cannot use ref or

3条回答
  •  鱼传尺愫
    2020-12-09 08:03

    This is not possible.

    The compiler will transform all local variables and parameters used by anonymous methods into fields in an automatically generated closure class.

    The CLR does not allow ref types to be stored in fields.

    For example, if you pass a value type in a local variable as such a ref parameter, the value's lifetime would extend beyond its stack frame.

提交回复
热议问题