“错误:并非所有代码路径都返回值." [英] "Error: Not all code paths return a value."

查看:49
本文介绍了“错误:并非所有代码路径都返回值."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在编译时会引发名义上的异常.我不明白为什么会发生这种情况,因为经过大量搜索之后,似乎仅在没有退出返回语句的条件存在的情况下才会发生错误,而我认为我的代码是完全包含在内的.

  bool CheckExisting(){帐户loginAcc =新的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 =新的XmlDocument();xmlFile.Load(filePaths [i]);foreach(xmlFile.SelectNodes("//Account")中的XmlNode节点){字符串firstName = node.SelectSingleNode("FirstName").InnerText;字符串lastName = node.SelectSingleNode("LastName").InnerText;字符串address1 = node.SelectSingleNode("Address1").InnerText;字符串address2 = node.SelectSingleNode("Address2").InnerText;字符串postCode = node.SelectSingleNode("Postcode").InnerText;字符串telePhone = node.SelectSingleNode("Telephone").InnerText;字符串mobile = node.SelectSingleNode("Mobile").InnerText;帐户newAcc =新的Account();newAcc.firstName = firstName;newAcc.lastName = lastName;newAcc.address1 = address1;newAcc.address2 = address2;newAcc.postCode = postCode;newAcc.telephone = telePhone;newAcc.mobile =移动;loginAcc = newAcc;}if(txtFirstName.Text == loginAcc.firstName&& txtLastName.Text == loginAcc.lastName){返回true;}别的{返回false;}返回false;}} 

解决方案

您的代码有效:

  bool CheckExisting(){//一些设置代码为(int i = 0; i< fCount; i ++){//与代码无关的代码返回 ...;}} 

现在,C#5语言规范的第8.8.3节讨论了 for 语句结尾的可达性:

如果满足以下至少一项,则可以到达 for 语句的端点:

  • for 语句包含一个可达的 break 语句,该语句退出 for 语句.
  • for 语句是可到达的,并且存在 for-condition ,并且不具有常量值 true .

在这种情况下,后者是正确的,因此 for 语句的末尾是可到达的……这就是方法的末尾.具有非无效返回类型的方法的结尾永远无法到达.

请注意,即使 human 可以检测到您永远无法到达for语句的末尾,情况也是这样.例如:

  bool Broken(){对于(int i = 0; i< 5; i ++){返回true;}//这仍然可以达到!} 

我们知道循环将始终至少执行一次,但是语言规则不会执行-因此语句的末尾是可到达的,并且会出现编译时错误.

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.

这篇关于“错误:并非所有代码路径都返回值."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆