C# 错误:并非所有代码路径都返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19903404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# error: not all code paths return a value
提问by NullReferenceException
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 program.
我希望它向主程序返回一个真值或假值。
But when I declare my method, public static void
, another error produces, a return keyword must not be followed by an object expression.
但是当我声明我的方法时,public static void
会产生另一个错误,返回关键字后面不能跟一个对象表达式。
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;
}
}
}
}
}
采纳答案by germi
Your method SearchFiles
only returns a value if isIdentical
is false
. If it's true
, the method never returns.
SearchFiles
如果isIdentical
is ,您的方法仅返回一个值false
。如果是true
,则该方法永远不会返回。
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.
对于你的第二个问题:如果你将你的方法声明为public static void
你不能有return
任何价值。void
意味着该方法不会给你任何回报。
You might want to have a look at this: Methods (C# Programming Guide), especially the part about return values.
您可能想看看这个:方法(C# 编程指南),尤其是关于返回值的部分。
Edit: Since you have your if / else
in a foreach
loop, you need something like this:
编辑:因为你有if / else
一个foreach
循环,你需要这样的东西:
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;
}
回答by Anand
public static Boolean SearchFiles(string path1, string path2)
{
if (isIdentical == false)
{
return false;
}
return true or false;
}
回答by David Pilkington
You need to return something if the if condition is not true
如果 if 条件不为真,你需要返回一些东西
you can try this
你可以试试这个
return isIdentical
and remove your if statement
并删除您的 if 语句
so that it looks like this
所以它看起来像这样
public class FileSearch
{
public static Boolean SearchFiles(string path1, string path2)
{
//Do your other work here
return isIdentical ;
}
}
回答by Farrukh Afzaal Kurd
public static Boolean SearchFiles(string path1, string path2)
{
if(isIdentical == false)
{
return false;
}
else
{
return true;
}
}
回答by Mohammad tanvirul islam
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;
}
}
回答by Kiran Waghmare
put this at the end of method
把它放在方法的末尾
return isIdentical;
返回是相同的;
u are not returning any value. This may work
你没有返回任何值。这可能有效