C# Error CS1643 – Not all code paths return a value in {0} of type ‘{1}’

C# Error

CS1643 – Not all code paths return a value in {0} of type ‘{1}’

Reason for the Error & Solution

Not all code paths return a value in method of type ‘type!’

This error occurs if a delegate body does not have a return statement, or has a return statement that the compiler is unable to verify will be reached. In the example below, the compiler does not attempt to predict the result of the branching condition in order to verify that the anonymous method block always returns a value.

Example

The following sample generates CS1643:

// CS1643.cs  
delegate int MyDelegate();  
  
class C  
{  
    static void Main()  
    {  
        MyDelegate d = delegate  
        {                 // CS1643  
            int i = 0;  
            if (i == 0)  
                return 1;  
        };  
    }  
}  

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...