Compiler Error CS1951

An expression tree lambda may not contain an out or ref parameter.

An expression tree just represents expressions as data structures. There is no way to represent specific memory locations as is required when you pass a parameter by reference.

To correct this error

  • The only option is to remove the ref modifier in the delegate definition and pass in the parameter by value.

Example

The following example generates CS1951:

// cs1951.cs
using System.Linq;
public delegate int TestDelegate(ref int i);
class Test
{
    static void Main()
    {
        System.Linq.Expressions.Expression<TestDelegate> tree1 = (ref int x) => x; // CS1951
    }
}

See Also

Concepts

Expression Trees