错误:并非所有代码路径都返回值

use*_*009 2 c# methods error-handling dictionary function

我编写了以下代码来计算XML文件的节点:

private Dictionary<string, int> ExtractNodeInfo(string fileContent)
    {
        XmlDocument xmlDocument;
        xmlDocument = new XmlDocument();
        xmlDocument.Load(fileContent);
        var ediNodes = xmlDocument.DocumentElement.SelectNodes("/EDI");
        Dictionary<string, int> nodeCount = new Dictionary<string, int>();
        foreach (XmlNode nodes in ediNodes)
        {
            FileManager.nodeRecurse(nodes, nodeCount);
        }

        foreach (var entry in nodeCount)
        {
            Console.WriteLine(entry.ToString());
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是它给了我以下错误:'XmlFileManager.FileManager.ExtractNodeInfo(string)':并非所有代码路径都返回一个值.

psu*_*003 6

你没有返回一个值.

在这种情况下,您需要在方法结束时使用return语句:

return nodeCount;
Run Code Online (Sandbox Code Playgroud)