【问题标题】:Unreachable Code Detected C#检测到无法访问的代码 C#
【发布时间】:2013-11-07 20:19:45
【问题描述】:

我试图发布无限数量的赞,但会根据数组中存储的 cookie 数量循环 cookie 和代理。显然 i++ 是无法访问的代码。这是什么原因?

public void PostLikes()
{
   PostLike postLike = new PostLike();
   for (int i =0;i<this.cookies.ToArray().Length;i++)
   {
      for (int j = 0; ; j++)
      {
         postLike.PostLike(this.cookies[i], this.importedProxies[i], this.proxyUsernameTextbox, this.proxyPasswordTextbox, this.postsIds[j]);
      }
   }
}

【问题讨论】:

  • cookies是什么类型?
  • 你为什么要把它标记为 C++ 和 Java?

标签: c# .net for-loop control-structure unreachable-code


【解决方案1】:

真正的问题是:

for (int j = 0; ; j++)

假设您内部没有任何其他控制语句(例如breakreturngotothrow),则产生一个无限循环

你可能打算这样做:

for (int j = 0; j < this.postsIds.Length; j++)
{
    ...
}

【讨论】:

    【解决方案2】:

    不要这样做

    for (int i =0;i<this.cookies.ToArray().Length;i++)
    

    因为

    this.cookies.toArray().Length
    

    它在 for 循环的每次迭代中进行评估,您每次都将 'this.cookies' 放入数组中,这样就可以得到它的长度? :) 你正在增加方​​法的复杂性

    【讨论】:

    • 这是一个列表。如何获取列表的长度?
    • @user2880751 见Count property 如果你好奇为什么列表有.Count 而数组有.Length,见stackoverflow.com/questions/300522/…
    • @user2880751 使用list.Count。将其存储在一个变量中并在循环中引用该变量,因此它只被读取一次。
    • @DavidArno AFAIK 优化器足够聪明,不需要额外的变量。如果循环中没有更改列表,则 list.Count 仅被引用一次。
    【解决方案3】:
    for (int j = 0; ; j++)
    

    这是一个死循环,所以不会到达 i++

    【讨论】:

      猜你喜欢
      • 2010-12-01
      • 2014-05-22
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多