C# Error CS1632 – Control cannot leave the body of an anonymous method or lambda expression

C# Error

CS1632 – Control cannot leave the body of an anonymous method or lambda expression

Reason for the Error & Solution

Control cannot leave the body of an anonymous method or lambda expression

This error occurs if a jump statement (break, goto, continue, etc.) attempts to move control out of an anonymous method block. An anonymous method block is a function body and can only be exited by a return statement or by reaching the end of the block.

The following sample generates CS1632:

// CS1632.cs  
// compile with: /target:library  
delegate void MyDelegate();  
class MyClass  
{  
   public void Test()  
   {
      for (int i = 0 ; i < 5 ; i++)  
      {  
         MyDelegate d = delegate {  
            break;   // CS1632  
          };
      }  
   }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...