簡體   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