未到达嵌套if语句的部分

时间:2015-11-06 14:16:02

标签: c# if-statement foreach

我有点不知道为什么下面这个if语句中的highestOrderLowestTradeVersion行没有被击中;在调试时,上述变量从不存在于if语句上下文中。即使下面的最后两个if语句都是真的,它也只是忽略了highestOrderLowestTradeVersion行。我已经尝试在列表中更高的位置移动变量声明,而不是初始化它,并在声明它之后单独初始化它,但我仍然无法获得该变量的值。我的代码下面有什么明显的错误吗?

int latestOrderVersion;
int latestTradeVersion;

int lowestOrderVersion = 0;
int lowestOrderHighestTradeVersion = 0;

int highestOrderVersion = 0;
int highestOrderHighestTradeVersion = 0;
int highestOrderLowestTradeVersion = 0;

foreach (DataRow filterResult2 in filterResults2.Rows)
{
    latestOrderVersion = Convert.ToInt32(filterResult2["OrderVersion"]);
    latestTradeVersion = Convert.ToInt32(filterResult2["TradeVersion"]);

    if (lowestOrderVersion == 0 || latestOrderVersion < lowestOrderVersion)
    {
        lowestOrderVersion = latestOrderVersion;

        if (lowestOrderHighestTradeVersion == 0 || lowestOrderHighestTradeVersion < latestTradeVersion)
        {
            lowestOrderHighestTradeVersion = latestTradeVersion;
        }
    }


    if (highestOrderVersion == 0 || latestOrderVersion > highestOrderVersion || latestOrderVersion == highestOrderVersion)
    {
        if (latestOrderVersion != highestOrderVersion)
        {
            highestOrderVersion = latestOrderVersion;
            highestOrderLowestTradeVersion = 0;    // never gets hit
        }

        if (highestOrderHighestTradeVersion == 0 || latestTradeVersion < highestOrderHighestTradeVersion)
        {
            highestOrderLowestTradeVersion = latestTradeVersion;    // never gets hit
        }
    }
}

1 个答案:

答案 0 :(得分:1)

原因是您处于优化开启的配置下。

您永远不会使用此变量,因此编译器在编译时会将其删除。

调试模式下的默认设置为OFF

发布模式下的默认设置为开启

关闭优化或在其他地方使用。

项目 - &gt;财产 - &gt;构建 - &gt;取消选中Optimize code