为什么这句话是错误的? (C#)

时间:2016-09-08 03:33:49

标签: c# if-statement logical-operators

请参阅链接:

http://i.imgur.com/gFcamd8.png

注意底部的Autos窗口显示toParse =""。然而toParse!=""无论如何,评估为真,导致应用程序崩溃。

这是完整的方法:

public void parseString(string toParse)
    {
        while (toParse != "")
        {
            string nextLine = readLine(ref toParse);

            if (nextLine.IndexOf("//") == 0)
            {
                comments.Add(nextLine);
                continue;
            }

            if (nextLine.IndexOf(".") == 0)
            {
                string[] declarationParts = nextLine.Split(' ');
                string declarationString = declarationParts[0].Substring(1, declarationParts[0].Length - 1);
                declarationString = char.ToUpper(declarationString[0]) + declarationString.Substring(1);
                DirectiveEnum type = (DirectiveEnum)Enum.Parse(typeof(DirectiveEnum), declarationString);
                string[] attributes = declarationParts.Skip(1).ToArray();
                MSILNode newNode = new MSILNode(type);
                newNode.addAttributes(attributes);
                toParse = toParse.Trim();
                if (toParse != "")
                {
                    while (toParse[0] != '{' && toParse[0] != '.' && toParse.IndexOf("//") != 0)
                    {
                        nextLine = readLine(ref toParse);
                        attributes = nextLine.Split(' ');
                        newNode.addAttributes(attributes);
                    }

                    if (toParse[0] == '{')
                    {
                        readLine(ref toParse);
                        string inside = separate(ref toParse, "}");

                        newNode.parseString(inside);
                    }
                }
                subNodes.Add(newNode);
                continue;
            }

            Console.WriteLine(nextLine);
        }
    }

2 个答案:

答案 0 :(得分:2)

只有在给出这一个快照时,很难看到调试会话期间发生的所有事情。但是,由于toParse是通过引用函数readline()(第57行)传递的,因此可以在该函数的主体中更改它的值。

从原始问题中提供的PNG图像:

53 if (toParse != "")
54 {
55     while (toParse[0] != '{' && toParse[0] != '.' && toParse.IndexOf("//") != 0)
56     {
57         nextLine = readLine(ref toParse);
58         ...

在第53行,toParse不为空。然后,在while循环的一次迭代期间,它被更新为空值。这将导致对数组索引的任何访问(即while条件中的toParse[0])抛出异常。

有关C#中ref关键字的详细信息,请参阅this StackOverflow issuethis official Microsoft documentation

我希望这有帮助!

答案 1 :(得分:0)

在图像中,此行import org.json4s._ import org.json4s.JsonDSL._ import org.json4s.jackson.JsonMethods._ object Json4sTest { def main(arg: Array[String]) { var json = parse("""{"name":"luca", "id": "1q2w3e4r5t", "age": 26, "inner": { "age": 27 }, "url":"http:// www.nosqlnocry.wordpress.com"}""") println(pretty(json)) val a: List[Map[String, JValue]] = List(Map("inner/age" -> 35, "age" -> 27), Map("name" -> "foo")) val r = jsonFieldUpdater(json, a) println(pretty(r)) val b = List(Updater("inner/age", 35), Updater("age", 27), Updater("name", "foo")) val s = jsonFieldUpdater2(json, b) println(pretty(s)) } def jsonFieldUpdater(json: JValue, list: List[Map[String, JValue]]): JValue = list.foldLeft(json) { case (json, kvs) => kvs.foldLeft(json) { case (json, (key, value)) => json.replace(key.split("/").toList, value) } } case class Updater(key: String, value: JValue) { def path: List[String] = key.split("/").toList } def jsonFieldUpdater2(json: JValue, list: List[Updater]): JValue = list.foldLeft(json) { case (json, u) => json.replace(u.path, u.value) } } 突出显示,表示它在Callstack中并在崩溃前被调用。这条线可能正在变为toParse""。

相关问题