繁体   English   中英

c#Visual Studio似乎遵循错误的代码路径

[英]c# Visual Studio appears to follow an incorrect code path

以下是摘自一个相当大的应用程序的一小段代码,以简化我的问题。 我之前提交过这个问题,但是措辞不佳导致我在设法编辑问题之前将其关闭。

这是当前的片段,我在其中添加了一些日志记录行。

int i = 0; 
Console.WriteLine("Before brackets");
if (i < 0) 
{ 
      Console.WriteLine("Inside brackets");
      return MyArray[i]; 
} 

当我用VS调试时,我看到:

i set to 0
if evaluates as false (when I hover over it in VS) 
In Output: Before brackets

然后,调试器进入括号内,并执行return MyArray[i] ,但是当我逐步进入return MyArray[i]行时,我在输出中没有看到Inside brackets

这种行为对我来说显然是错误的,我想知道是否有人遇到过这种情况。

我在装有VS10和.Net4.0的Windows XP 64位计算机上。

月亮

附加1

Henk要求我提供一个“控制台应用程序”,下面已完成。 但是,正如我怀疑的那样,它不显示此问题。 我相信还有其他问题(线程?)在我的真实应用程序中引起了我的问题-我显然可以发布该问题。 我知道我没有给您一个明确定义的问题-如果可以的话,我会的。 这就是为什么我要问这个问题-如果它碰到有人有类似东西的绳索。 为了井井有条,这是没有出现问题的控制台版本。 我觉得在这里穿上它会加剧混乱...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication2
{
    class Program
    {
        static string[] Mods = new string[] { "Cat", "Dog", "Geek" };

        static void Main(string[] args)
        {
            string t = "Geek";
            Console.WriteLine("Answer: " + FindD(t));

        }

        public static string FindD(string ModelFullName)
        {
            int ix = Array.IndexOf<string>(Mods, ModelFullName);

            Console.WriteLine("ix: " + ix + " = " + (ix < 0).ToString());
            if (ix < 0)
            {
                Console.WriteLine("2ix: " + ix + " = " + (ix < 0).ToString());
                Error.Process(ErrorLevel.Critical, "ModelName not found: " + ModelFullName);
            }
            try
            {

                return Mods[ix];
            }
            catch (Exception)
            {
                Error.Process(ErrorLevel.Critical, "Could not point to Mod for: " + ModelFullName);
            }

            return null;
        }

        enum ErrorLevel { Note, Critical };
        class Error
        {
            public static void Process(ErrorLevel EL, string message)
            {
                if (EL == ErrorLevel.Critical)
                {
                    throw new Exception("Critical error: " + GetStackTrace() + message);
                }
            }

            public static string GetStackTrace()
            {
                StackTrace stackTrace = new StackTrace();           // get call stack
                StackFrame[] stackFrames = stackTrace.GetFrames();  // get method calls (frames)

                string st = "";
                // write call stack method names
                for (int i = 6; i > 1; i--)
                {
                    StackFrame stackFrame = stackFrames[i];
                    st = st + stackFrame.GetMethod().Name + "/";
                }

                return st;
            }
        }
    }
}

附加2

看来我的问题不是仅VS调试器执行错误路径的执行路径之一。 即看起来好像我要放在方括号内,因为调试器在最后一条语句上执行,但实际上得到的结果是正确的。

从Google网页搜索中找到提示后,我设法解决了该问题。 看来我在"Optmise Code"模式下运行调试器。 这是我早先检查过的内容,但是我没有发现什么,尽管我在调试模式下取消选择了此选项-我在标准工具栏中选择了“发布”。 当我切换到“ Debug ”时,问题就消失了。

我一定想念什么,但这很合逻辑吗?

  • 我设置为零。
    • 我不小于零。
    • 不输出内括号。

这是合乎逻辑的,不是吗? 使用调试器单步执行代码。 您会发现这很有意义。

您确定您的if语句不如下:

if (i < 0)
    Console.WriteLine(...);
    return MyArray(i);

请注意误导性缩进和缺少括号。

如果i = 0i < 0为假。 如果将条件设置为i <= 0 ,那么它将执行条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM