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

c#Visual Studio似乎遵循了错误的代码路径,c#,visual-studio-2010,path,C#,Visual Studio 2010,Path,下面是一个取自一个相当大的应用程序的小片段,以简化我的问题。我以前提交过这个问题,但我拙劣的措辞导致它在我试图编辑我的问题之前就被关闭了 这是我添加了一些日志行的当前代码段 int i = 0; Console.WriteLine("Before brackets"); if (i < 0) { Console.WriteLine("Inside brackets"); return MyArray[i]; } 然后,调试器在括号内执行,并执行返回MyA

下面是一个取自一个相当大的应用程序的小片段,以简化我的问题。我以前提交过这个问题,但我拙劣的措辞导致它在我试图编辑我的问题之前就被关闭了

这是我添加了一些日志行的当前代码段

int i = 0; 
Console.WriteLine("Before brackets");
if (i < 0) 
{ 
      Console.WriteLine("Inside brackets");
      return MyArray[i]; 
} 
然后,调试器在括号内执行,并执行
返回MyArray[i]
,但是在我进入
返回MyArray[i]
行之前,我在输出中没有看到
括号内的

这种行为(对我来说)显然是错误的,我想知道是否有其他人遇到过类似的事情

我在一台64位Windows XP机器上,使用VS10和.Net4.0

月亮

附加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;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统诊断;
命名空间控制台应用程序2
{
班级计划
{
静态字符串[]Mods=新字符串[]{“猫”、“狗”、“极客”};
静态void Main(字符串[]参数)
{
字符串t=“Geek”;
控制台写线(“回答:+FindD(t));
}
公共静态字符串FindD(字符串ModelFullName)
{
int ix=Array.IndexOf(Mods,ModelFullName);
Console.WriteLine(“ix:+ix+”=“+(ix<0.ToString());
如果(ix<0)
{
Console.WriteLine(“2ix:+ix+”=“+(ix<0.ToString());
Error.Process(ErrorLevel.Critical,“找不到ModelName:+ModelFullName”);
}
尝试
{
返回Mods[ix];
}
捕获(例外)
{
Error.Process(ErrorLevel.Critical,“无法指向模块:”+ModelFullName);
}
返回null;
}
枚举错误级别{注意,严重};
类错误
{
公共静态无效进程(错误级别EL,字符串消息)
{
如果(EL==ErrorLevel.Critical)
{
抛出新异常(“严重错误:+GetStackTrace()+消息);
}
}
公共静态字符串GetStackTrace()
{
StackTrace StackTrace=new StackTrace();//获取调用堆栈
StackFrame[]stackFrames=stackTrace.GetFrames();//获取方法调用(帧)
字符串st=“”;
//写入调用堆栈方法名
对于(int i=6;i>1;i--)
{
StackFrame StackFrame=stackFrames[i];
st=st+stackFrame.GetMethod().Name+“/”;
}
返回st;
}
}
}
}
额外2个


我的问题似乎不是执行路径走错了路线,而仅仅是VS调试器。i、 看起来好像我在括号内,因为调试器在最后一条语句上执行步骤,但我实际得到的结果看起来是正确的

如果
i=0
i<0
为假。如果将条件设置为
i如果
i=0
i<0
为假。如果你将你的条件设置为
i,我一定是遗漏了什么,但这是很合乎逻辑的

  • i设置为零。
    • 我不少于 零
    • 括号内不是 输出
这是合乎逻辑的,不是吗?
使用调试器逐步完成代码。你会发现这是有道理的。

我肯定遗漏了什么,但这是很合乎逻辑的

  • i设置为零。
    • 我不少于 零
    • 括号内不是 输出
这是合乎逻辑的,不是吗?
使用调试器逐步完成代码。你会发现它是有道理的。

你确定你的if语句不是如下所示:

if (i < 0)
    Console.WriteLine(...);
    return MyArray(i);
if(i<0)
控制台写入线(…);
返回MyArray(i);

注意误导性的缩进和括号的缺失。

您确定您的if语句不是如下所示:

if (i < 0)
    Console.WriteLine(...);
    return MyArray(i);
if(i<0)
控制台写入线(…);
返回MyArray(i);

请注意错误的缩进和括号的缺失。

我在谷歌网络搜索中找到了一个提示,之后我成功地解决了这个问题。似乎我是在
的“Optmise code”
模式下运行调试器的。这是我早些时候检查过的,但我没有发现的是,尽管我在调试模式中取消了这个选项,但我在标准工具栏中选择了“Release”。当我切换到“
Debug
”时,问题就消失了。

我在google web搜索中找到了一个提示后,成功地解决了这个问题。似乎我是在
的“Optmise code”
模式下运行调试器的。这是我早些时候检查过的,但我没有发现的是,尽管我在调试模式中取消了这个选项,但我在标准工具栏中选择了“Release”。当我切换到“
Debug
”时,问题消失了。

@manimoon-
I<0
将在
I==0
时计算为false?我不确定问题是什么。0不小于0,因此
if
语句的计算结果为false,而不计算其中的代码。因此,在输出中看不到“内括号”,这是正确的。我看没问题