代码执行是如何进入这个'如果'声明?

时间:2014-11-18 16:15:13

标签: c# string http url startswith

请查看以下代码:

    public static string GetCleanURL(string baseURL, string url)
    {
        string cleanUrl = url.ToLower();
        if (cleanUrl.StartsWith("http://"))
        {//It already starts with http://  It is already in the correct form return it.
            return cleanUrl;
        }

' url'传入的值是" 123.123.123.123:1234 / myurl / withstuff.xml"。在' if'陈述,' cleanUrl'的价值是" 123.123.123.123:1234 / myurl / withstuff.xml"。但由于某种原因,代码执行在if块内部,并且返回cleanUrl;'被执行。

以下是' cleanUrl':

的当前值的屏幕截图

enter image description here

当我将cleanUrl.StartsWith("http://")插入'立即窗口'我的调试器,它返回false。这是我期望的。但是,执行会以某种方式进入if块,就像它返回true一样。

有人可以解释一下这是怎么回事吗?

2 个答案:

答案 0 :(得分:2)

已解决!!!

我感谢那些帮助过我的人。

在代码库和调试内容实际同步之前,我需要清理并重建我的项目并关闭并重新打开Visual Studio 2013大约4次。它现在似乎工作正常。

不确定为什么会这样,或者为什么我需要在事情同步之前多次进行清洁/重建。但它现在正在运作。

所以,朋友们,如果你发现你的代码只是表现得很疯狂而没有做它应该做的事情。只要意识到任何心智正常的人永远不会成为程序员。然后做几次清洁/重建并祈祷奇怪的消失永远不会回来。

感谢您对此提供的所有帮助。

我喜欢与开发工具斗争......

答案 1 :(得分:1)

不,StartWith没有错误,并按预期工作。

尝试下面的最小代码。将代码减少到最小量以重新解决问题。这种情况每次都会发生吗? 是否会出现多个候选字符串?或者只是那个字符串?

其他事情很抱歉,以下是Doesn't start

    static void Main(string[] args)
    {
        string cleanUrl = "123.123.123.123:1234/SomeFile.xml";
        if (cleanUrl.StartsWith("http://"))
            Console.WriteLine("Starts");
        else
            Console.WriteLine("Doesn't start");
        Console.ReadLine();
    }