Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#错误:并非所有代码路径都返回值_C# - Fatal编程技术网

C#错误:并非所有代码路径都返回值

C#错误:并非所有代码路径都返回值,c#,C#,我在另一个类中声明了一个方法,它有一个错误“并非所有代码路径都返回值” 我想它返回一个真或假值的主程序 但是当我声明我的方法时,publicstaticvoid,会产生另一个错误,return关键字后面不能跟对象表达式 public class FileSearch { public static Boolean SearchFiles(string path1, string path2) { bool isIdentical = false;

我在另一个类中声明了一个方法,它有一个错误“并非所有代码路径都返回值”

我想它返回一个真或假值的主程序

但是当我声明我的方法时,
publicstaticvoid
,会产生另一个错误,return关键字后面不能跟对象表达式

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

您的方法
SearchFiles
仅在
isIdentical
false
时返回值。如果为
true
,则该方法永远不会返回

要删除此错误,请编写如下内容:

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;
    }
}
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;
}
关于第二个问题:如果将方法声明为
publicstaticvoid
,则不能
返回任何值
void
表示该方法不会返回任何内容

您可能想看一下:,尤其是关于返回值的部分

编辑:由于您的
if/else
处于
foreach
循环中,因此您需要如下内容:

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

如果if条件不为true,则需要返回一些内容

你可以试试这个

return isIdentical 
并删除您的if语句

所以看起来像这样

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

你可以试试这个代码……我想它会帮你的

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

    }

把这个放在方法的末尾

返回身份

u没有返回任何值。
这可能会起作用

您可以放置else部分并从其返回布尔值,在这种情况下,您不会向我们显示所有代码。我写的方法会起作用。我有foreach循环,如果语句在该循环中,如果它在foreach循环中,您需要在循环后返回一些内容,以防在空集合上循环。请确切解释使用此方法要实现什么。现在,在第一次比较后返回一个值。是否要获取所有相同的文件?使用
SearchFiles
方法的目的是什么?我的直觉是,一旦发现两个不同的文件,就要返回
false
,如果所有文件(但不应该只比较同名文件?)都相同,则返回
true
。如果是这样,则正确放置
返回假
,但
返回真
应放置在循环之后。可能存在重复的