HomeCSharpC# 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}’

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...