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
。如果是这样,则正确放置返回假
,但返回真
应放置在循环之后。可能存在重复的