简体   繁体   English

并非所有代码路径都返回值C#?

[英]Not all code paths return a value C#?

This is my code: 这是我的代码:

public String[] readXML(String filename)  
{  
    XmlReader xmlReader = XmlReader.Create(@filename);  
    List<String> names = new List<string>();
    String[] keywords = null;
    while (xmlReader.Read())  
    {  
        //Keep reading  
        if (xmlReader.Name.Equals("Keyword") && (xmlReader.NodeType == XmlNodeType.Element))  
        {  
            // get attribute from the Xml element here  
            string keywords = xmlReader.GetAttribute("name");  
            names.Add(keywords);  
            String[] keywordsArray = names.ToArray();  
        }  
        else
        {
            MessageBox.show("An Error Occured");
        }
    }  
    return keywordsArray;
}

Would this do? 这样可以吗? Can someone test this? 有人可以测试吗?

I think it is because your return statement is inside the if inside the loop. 我认为这是因为您的return语句位于if循环内。 What happens if the loop never executes, or the if never is true? 如果循环永不执行,或者如果永不为真,会发生什么?

Do you understand you own code? 您了解自己的代码吗? What do you want to return from this method? 您要从此方法返回什么? Currently your code means following: 当前,您的代码含义如下:
Read XML file until "Keyword" element will not be found. 读取XML文件,直到找不到“关键字”元素。 After "Keyword" element will be found take value of it's "name" attribute and return it wrapped into one element array. 找到“关键字”元素后,取其“名称”属性的值,并将其包装在一个元素数组中返回。
As others have mentioned there can be case when XML document will not have element. 正如其他人提到的那样,XML文档中可能没有元素。 In that case you will exit your loop with no value returned. 在这种情况下,您将退出循环,不返回任何值。 You should decide what to do in that case. 在这种情况下,您应该决定要做什么。 You can return some special value like "null" or empty array. 您可以返回一些特殊值,例如“ null”或空数组。 Or if you are sure that at least one "Keyword" element should exist in specified XML document then you can throw some Exception from the end of method to indicate that some pre-condition was violated. 或者,如果您确定指定的XML文档中至少应存在一个“关键字”元素,则可以从方法末尾引发Exception,以表明某些先决条件被违反。
I think you've tried to write method with different behavior. 我认为您尝试编写具有不同行为的方法。 I believe you've meant following: 我相信您的意思是:
Read XML file and collect values of "name" attribute from all "Keyword" elements and return collected values as string array. 读取XML文件并从所有“关键字”元素中收集“名称”属性的值,并将收集的值作为字符串数组返回。
That will be more meaningful and in that case you will always have some result even when XML file will not contain any "Keyword" element. 这将更有意义,在这种情况下,即使XML文件不包含任何“关键字”元素,您也始终会得到一些结果。 To achieve such behavior you should move list to array conversion and return statement to the end of method . 要实现这种行为,您应该将列表移动到数组转换,然后将语句返回到方法的末尾 You will have code like this: 您将具有以下代码:

public static String[] readXML(String filename)
{
    using(XmlReader xmlReader = XmlReader.Create(@filename))
    {
        List<String> names = new List<string>();
        while(xmlReader.Read())
        {
            //Keep reading  
            if(xmlReader.Name.Equals("Keyword") && (xmlReader.NodeType == XmlNodeType.Element))
            {
                // get attribute from the Xml element here  
                string keywords = xmlReader.GetAttribute("name");
                names.Add(keywords);
            }
        }
        String[] keywordsArray = names.ToArray();
        return keywordsArray;
    }
}

Please note that I've also wrapped usage of created XmlReader in "using" block to ensure that it will be disposed before we will leave the method. 请注意,我还将使用XmlReader创建的用法包装在“使用”块中,以确保在我们离开该方法之前将其丢弃。

You should have a default return for you while and if at the end of the method. 如果在方法末尾,则应该为您提供默认返回值。

As the compiler says, you should have a return statement for all code paths. 就像编译器所说的那样,您应该对所有代码路径都有一个return语句。 If the while never executes, not return, and the same for the if statement. 如果while永远不执行,则不返回,对于if语句也是如此。

You have to put a return statement outside the loop. 您必须在循环外放置一个return语句。 Otherwise if the name is never "Keyword", your code will never return! 否则,如果名称永远不是“关键字”,那么您的代码将永远不会返回!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM