检测不到C#.NET代码 [英] Unreachable Code Detected C#.NET

查看:136
本文介绍了检测不到C#.NET代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个用于Google地图的抓取代码,并且对"return result2;"发出警告. wat可能是错误的,因为它不起作用或无法返回结果?

I have this scraper code for google maps and am gettin a warnin on " return result2;" wat could be wrong because its not working or returnin results??

if (this.CanAddRes(result, mapScrapeResult))
            {
                Monitor.Enter(result);
                try
                {
                    result.Add(mapScrapeResult);
                }
                finally
                {
                    Monitor.Exit(result);
                }
                if (this.terminated)
                {
                    result2 = false;
                    return result2;
                }
                if (base.Callback != null)
                {
                    base.Callback.Process(mapScrapeResult);
                }
            }
        }
    }
    return text.IndexOf("</div>Next</a>") >= 0;
    return result2;
}

推荐答案

不是很明显吗?

您连续有两个return语句.第二个return语句将永远不会执行,因为您已经在第一个返回值中返回了布尔值.

return语句立即将控制权返回给调用者.此后将不执行该方法中的其他代码.

如果您看不到这一点,我强烈建议您阅读一本有关C#的初学者书籍并进行研究.这是您错过的一个非常基本的概念.
Isn''t it obvious??

You have two return statements in a row. The second return statment will never be executed becuase you already returned a boolean value with the first return.

The return statement returns control to the caller immediately. No other code in the method will be executed after that.

If you couldn''t see this, I HIGHLY suggest picking up a beginners book on C# and working through it. This is a very basic concept you missed.


除了正确的Dave答案:

我要补充一点,您真的想以一种无警告的方式工作,每次有任何警告时都要修正代码.您可以使用警告来工作,但这就像离开破碎的窗户一样.如果累积警告,则将需要忽略所有警告,因此有价值的警告机制实际上将变得无用.此外,这太过压力了.

—SA
In addition to the correct Dave answer:

I would add you really want to work in a warning-free manner, fixing the code each time you have any warnings. You can work with warning but this is like leaving with broken windows. If you accumulate warnings, you will need to ignore all of them, so the valuable warning mechanism will be rendered virtually useless. Besides, this is too stressful.

—SA






return text.IndexOf("</div>Next</a>") >= 0;
 return result2;




第二个return语句不会在此代码中执行,这就是原因




second return statement wont execute in this code that is the reason


这篇关于检测不到C#.NET代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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