C#-fout: niet alle codepaden retourneren een waarde

Ik heb een methode in een andere klasse gedeclareerd en deze heeft de fout “niet alle codepaden retourneren een waarde”

Ik zou graag willen dat het een true of false waarde teruggeeft aan het hoofdprogramma.

Maar wanneer ik mijn methode declareer, public static void, levert er een andere fout op, een return-sleutelwoord mag niet worden gevolgd door een objectexpressie.

public class FileSearch
{
    public static Boolean SearchFiles(string path1, string path2)
    {
        bool isIdentical = false;
        string content1 = null;
        string content2 = null;
        DirectoryInfo d1 = new DirectoryInfo(path1);
        DirectoryInfo d2 = new DirectoryInfo(path2);
        foreach (FileInfo f1 in d1.GetFiles("*.txt", SearchOption.AllDirectories))
        {
            foreach (FileInfo f2 in d2.GetFiles("*.txt", SearchOption.AllDirectories))
            {
                content1 = (File.ReadAllText(f1.DirectoryName + "\\" + f1));
                content2 = (File.ReadAllText(f2.DirectoryName + "\\" + f2));
                isIdentical = content1.Equals(content2, StringComparison.Ordinal);
                if (isIdentical == false)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    }
}

Antwoord 1, autoriteit 100%

Uw methode SearchFilesretourneert alleen een waarde als isIdenticalfalseis. Als het trueis, keert de methode nooit terug.

Om deze fout te verwijderen, schrijf je zoiets als dit:

public static Boolean SearchFiles(string path1, string path2)
{
    // do some work to assign a value to isIdentical
    // note that it would be idiomatic to just write "return isIdentical;" in this case
    // I keep it explicitly here for demonstration purposes 
    if (isIdentical == false)
    {
        return false;
    }
    else
    {
        return true;
    }
}

Naar uw tweede vraag: als u uw methode aangeeft als public static voidu mag niet returnelke waarde. voidbetekent dat de methode u niets terug zal geven.

Misschien wil je dit bekijken: Methoden (C # Programmeergids) , vooral het onderdeel over retourwaarden.

EDIT: Aangezien u uw if / elsein een foreachlus hebt, hebt u zoiets nodig:

public static Boolean SearchFiles(string path1, string path2)
{
    foreach(var item in collection)
    {
        // do some work to assign a value to isIdentical
        if (isIdentical == false)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    // in case the collection is empty, you need to return something
    return false;
}

Antwoord 2, Autoriteit 69%

U moet iets retourneren als de voorwaarde niet waar is

U kunt dit proberen

return isIdentical 

en verwijder uw if-statement

zodat het er zo uitziet

public class FileSearch
{
        public static Boolean SearchFiles(string path1, string path2)
        {
            //Do your other work here
            return isIdentical ;
        }
}

Antwoord 3, Autoriteit 15%

U kunt deze code proberen …. Ik denk dat het u zal helpen.

public class FileSearch
    {
        public static Boolean SearchFiles(string path1, string path2)
        {
            bool isIdentical = false;
            string content1 = null;
            string content2 = null;
bool result=false;
            DirectoryInfo d1 = new DirectoryInfo(path1);
            DirectoryInfo d2 = new DirectoryInfo(path2);
            foreach (FileInfo f1 in d1.GetFiles("*.txt", SearchOption.AllDirectories))
            {
                foreach (FileInfo f2 in d2.GetFiles("*.txt", SearchOption.AllDirectories))
                {
                    content1 = (File.ReadAllText(f1.DirectoryName + "\\" + f1));
                    content2 = (File.ReadAllText(f2.DirectoryName + "\\" + f2));
                    isIdentical = content1.Equals(content2, StringComparison.Ordinal);
                    if (isIdentical == false)
                    {
                       break;
                    }
                    else
                    {
                        result=true;break;
                    }
break;
            }
return result;
        }
    }

Antwoord 4, Autoriteit 8%

public static Boolean SearchFiles(string path1, string path2)
{
    if (isIdentical == false)
    {
        return false;
    }
    return true or false;
}

Antwoord 5, Autoriteit 8%

public static Boolean SearchFiles(string path1, string path2)
{
    if(isIdentical == false)
    {
        return false;
    }
    else
    {
        return true;
    }
}

Antwoord 6

zet dit aan het einde van de methode

retour is identiek;

u retourneert geen waarde.
Dit kan werken

Other episodes