&“错误:并非所有代码路径都返回值."

编程入门 行业动态 更新时间:2024-10-26 18:26:21
本文介绍了&“错误:并非所有代码路径都返回值."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的代码在编译时抛出名义异常.我不明白为什么会发生这种情况,因为经过广泛搜索后,错误发生的原因似乎只是在没有退出返回语句的情况下存在,但我认为我的代码是完全包容的.

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 reachable break statement that exits the for statement.
  • The for statement is reachable and a for-condition is present and does not have the constant value true.

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.

更多推荐

&amp;“错误:并非所有代码路径都返回值."

本文发布于:2023-11-10 01:18:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   返回值   错误   代码   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!