This documentation is archived and is not being maintained.

Compiler Error CS1706

Expression cannot contain anonymous methods or lambda expressions

You cannot insert an anonymous method inside an expression.

To correct this error

  • Use a regular delegate in the expression.

The following example generates CS1706.

// CS1706.cs
using System;

delegate void MyDelegate();
class MyAttribute : Attribute
{
    public MyAttribute(MyDelegate d) { }
}

// Anonymous Method in Attribute declaration is not allowed.
[MyAttribute(delegate{/* anonymous Method in Attribute declaration */})]  // CS1706
class Program
{
}
Show: