-4

I have the following error:

Not all code paths return a value in lambda expression of type.

I can't resolve it.

delegate bool BoolPassword(string s1, string s2);
delegate bool Captha(string s1, string s2);

static void Main(string[] args)
{
    Console.Write("Enter password ");
    string password1 = Console.ReadLine();
    Console.Write("Repeat password ");
    string password2 = Console.ReadLine();

    BoolPassword bp = (s1, s2) => s1 == s2;

    if (bp(password1, password2))
    {
        Random rand = new Random();

        string resCaptha = "";

        for (int i = 0; i < 10; i++)
            resCaptha += (char)rand.Next(0, 100);

        Console.WriteLine("Enter code " + resCaptha);
        string resCode = Console.ReadLine();

        Captha cp = (s1, s2) => // Error is here
            {                        
                if (s1 == s2)
                    Console.WriteLine("Ok");
                else
                    Console.WriteLine("fail");                        
            };

        cp(resCaptha, resCode);
    }
    else
        Console.WriteLine("Fail");
}
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Spiderman5
  • 23
  • 5

4 Answers4

4

This code does not return a bool value as defined by your delegate

Captha cp = (s1, s2) => // Error is here
    {                        
        if (s1 == s2)
            Console.WriteLine("Ok");
        else
            Console.WriteLine("fail");                        
    };

You can fix this by doing:

Captha cp = (s1, s2) => // Error is here
    {                        
        if (s1 == s2)
            Console.WriteLine("Ok");
        else
            Console.WriteLine("fail");                        

        return s1 == s2;
};

or by amending your delegate signature:

delegate void Captha(string s1, string s2);
dav_i
  • 27,509
  • 17
  • 104
  • 136
2

Your delegate to Gaptcha returns a bool, whilst your inline function does not. Change either your lambda to the following

Captha cp = (s1, s2) => // Error is here
{                        
    if (s1 == s2) {
        Console.WriteLine("Ok");
        retrurn true;
    }
    else {
        Console.WriteLine("fail");                        
        return false;
    }
};

var check = cp(resCaptha, resCode);

or the delegate-type to return void.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
1

Your lambda expression is expected to return a bool. It does not. Either change the delegate signature or return a bool.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

You've stated in your code that the return type of the lambda is bool but you're not actually returning anything from your function.

delegate bool Captha(string s1, string s2)

If you want this to be your return type, you need to return something in all branches of the if statement.

Captha cp = (s1, s2) => // Error is here
            {                        
                if (s1 == s2)
                   return true;
                else
                   return false;                       
            };

You could also shorten this to return s1 == s2;

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162