vendredi 10 avril 2020

“not all code path return a value” error occurs in a method, but I did use some if statements to cover all scenario

This may be a stupid question, but I just want to somebody can give a better explanation.

I have a method defined as below:

    private int Test(int i)
    {
        if (i < 0) return -1;
        if (i == 0) return 0;
        if (i > 0) return 1;

        //return 0;        
    }

It gives me this error "not all code path return a value".

I thought I had 3 if statement, which could cover all the scenarios(i<0, i==0, i>0). So it should not show me this error.

Aucun commentaire:

Enregistrer un commentaire