Compiler Error CS1946


Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

An anonymous method expression cannot be converted to an expression tree.

An anonymous method represents a set of statements but an expression tree must not contain a statement. Therefore an anonymous method cannot be represented by an expression tree.

To correct this error

  1. Change the anonymous method to a lambda expression.

The following example generates CS1946:

// cs1946.cs  
using System;  
    using System.Linq.Expressions;  
    public delegate void D();  
    class Test  
        static void Main()  
            Expression<D> tree = delegate() { }; //CS1946  
            // Try using a lambda expression instead.  
            // Expression<D> tree = (x) => x + 1;  

Anonymous Methods
Expression Trees