C# error: not all code paths return a value

前端 未结 6 895
情深已故
情深已故 2020-12-10 23:27

I have declared a method in another class and it has an error \"not all code paths return a value\"

I would like it to return a true or false value to the main progr

相关标签:
6条回答
  • 2020-12-11 00:09
    public static Boolean SearchFiles(string path1, string path2)
    {
        if (isIdentical == false)
        {
            return false;
        }
        return true or false;
    }
    
    0 讨论(0)
  • 2020-12-11 00:15

    you can try this code....i think it will help you.

    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;
            }
    
        }
    
    0 讨论(0)
  • 2020-12-11 00:18

    You need to return something if the if condition is not true

    you can try this

    return isIdentical 
    

    and remove your if statement

    so that it looks like this

    public class FileSearch
    {
            public static Boolean SearchFiles(string path1, string path2)
            {
                //Do your other work here
                return isIdentical ;
            }
    }
    
    0 讨论(0)
  • 2020-12-11 00:20

    Your method SearchFiles only returns a value if isIdentical is false. If it's true, the method never returns.

    To remove this error, write something like this:

    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;
        }
    }
    

    To your second question: If you declare your method as public static void you must not return any value. void means that the method will not give you anything back.

    You might want to have a look at this: Methods (C# Programming Guide), especially the part about return values.

    Edit: Since you have your if / else in a foreach loop, you need something like this:

    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;
    }
    
    0 讨论(0)
  • 2020-12-11 00:24

    put this at the end of method

    return isIdentical;

    u are not returning any value. This may work

    0 讨论(0)
  • 2020-12-11 00:25
    public static Boolean SearchFiles(string path1, string path2)
    {
    
        if(isIdentical == false)
        {
            return false;
        }
        else
        {
            return true;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题