&“错误:并非所有代码路径都返回值." [英] "Error: Not all code paths return a value."
问题描述
我的代码在编译时抛出名义异常.我不明白为什么会发生这种情况,因为经过广泛搜索后,错误发生的原因似乎只是在没有退出返回语句的情况下存在,但我认为我的代码是完全包容的.
bool CheckExisting(){账户 loginAcc = new Account();字符串路径 = Application.StartupPath.ToString() + "\\Customers";int fCount = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories).Length;for(int i = 0;i<fCount;i++){String[] filePaths = Directory.GetFiles(Application.StartupPath + "\\Customers\\");XmlDocument xmlFile =new XmlDocument();xmlFile.Load(filePaths[i]);foreach(xmlFile.SelectNodes 中的 XmlNode 节点("//Account")){string firstName = node.SelectSingleNode("FirstName").InnerText;string lastName = node.SelectSingleNode("LastName").InnerText;string address1 = node.SelectSingleNode("Address1").InnerText;string address2 = node.SelectSingleNode("Address2").InnerText;string postCode = node.SelectSingleNode("Postcode").InnerText;string telePhone = node.SelectSingleNode("Telephone").InnerText;string mobile = node.SelectSingleNode("Mobile").InnerText;账户 newAcc = new Account();newAcc.firstName = firstName;newAcc.lastName = lastName;newAcc.address1 = address1;newAcc.address2 = address2;newAcc.postCode = 邮政编码;newAcc.telephone = 电话;newAcc.mobile = 移动;loginAcc = newAcc;}if(txtFirstName.Text == loginAcc.firstName && txtLastName.Text == loginAcc.lastName){返回真;}别的{返回假;}返回假;}}
您的代码有效:
bool CheckExisting(){//一些设置代码for (int i = 0; i < fCount; i++){//不太相关的代码返回 ...;}}
现在 C# 5 语言规范第 8.8.3 节讨论了 for
语句结尾的可达性:
如果至少满足以下一项,则 for
语句的终点是可达的:
for
语句包含一个可到达的break
语句,该语句退出for
语句.for
语句是可访问的,并且存在 for-condition 并且没有常量值true
.
在这种情况下后者是正确的,所以 for
语句的结尾是可到达的......这就是方法的结尾.永远无法到达具有非 void 返回类型的方法的结尾.
请注意,即使 人 可以检测到您永远无法到达 for 语句的末尾,情况也是如此.例如:
bool Broken(){for (int i = 0; i <5; i++){返回真;}//这仍然是可达的!}
我们知道循环将始终至少执行一次,但语言规则不会 - 因此语句的末尾是可到达的,并且您会收到编译时错误.>
My code, upon compilation, throws the titular exception. I don't understand why this happens because after extensive searching the reason the error occurs is seemingly only when conditions exist where there is no exit return statement, and yet I think my code is fully inclusive.
bool CheckExisting()
{
Account loginAcc = new Account();
string path = Application.StartupPath.ToString() + "\\Customers";
int fCount = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories).Length;
for(int i = 0;i<fCount;i++)
{
String[] filePaths = Directory.GetFiles(Application.StartupPath + "\\Customers\\");
XmlDocument xmlFile =new XmlDocument();
xmlFile.Load(filePaths[i]);
foreach(XmlNode node in xmlFile.SelectNodes("//Account"))
{
string firstName = node.SelectSingleNode("FirstName").InnerText;
string lastName = node.SelectSingleNode("LastName").InnerText;
string address1 = node.SelectSingleNode("Address1").InnerText;
string address2 = node.SelectSingleNode("Address2").InnerText;
string postCode = node.SelectSingleNode("Postcode").InnerText;
string telePhone = node.SelectSingleNode("Telephone").InnerText;
string mobile = node.SelectSingleNode("Mobile").InnerText;
Account newAcc = new Account();
newAcc.firstName = firstName;
newAcc.lastName = lastName;
newAcc.address1 = address1;
newAcc.address2 = address2;
newAcc.postCode = postCode;
newAcc.telephone = telePhone;
newAcc.mobile = mobile;
loginAcc = newAcc;
}
if(txtFirstName.Text == loginAcc.firstName && txtLastName.Text == loginAcc.lastName)
{
return true;
}
else
{
return false;
}
return false;
}
}
Your code is effectively:
bool CheckExisting()
{
// Some setup code
for (int i = 0; i < fCount; i++)
{
// Code which isn't terribly relevant
return ...;
}
}
Now the C# 5 language specification section 8.8.3 talks about the reachability of the end of a for
statement:
The end point of a
for
statement is reachable if at least one of the following is true:
- The
for
statement contains a reachablebreak
statement that exits thefor
statement.- The
for
statement is reachable and a for-condition is present and does not have the constant valuetrue
.
The latter is true in this case, so the end of the for
statement is reachable... and that's the end of the method. The end of a method with a non-void return type can never be reachable.
Note that this is the case even if a human could detect that you can never reach the end of the for statement. For example:
bool Broken()
{
for (int i = 0; i < 5; i++)
{
return true;
}
// This is still reachable!
}
We know that the loop will always execute at least once, but the language rules don't - therefore the end of the statement is reachable, and you get a compile-time error.
这篇关于&“错误:并非所有代码路径都返回值."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!