Below is a small snippet taken from a fairly large app to simplify my question. I submitted this question before, but my poor wording caused it to be closed before I managed to edit my question.
This is the current snippet to which I have added some logging lines.
int i = 0;
Console.WriteLine("Before brackets");
if (i < 0)
{
Console.WriteLine("Inside brackets");
return MyArray[i];
}
When I debug with VS I see:
i set to 0
if evaluates as false (when I hover over it in VS)
In Output: Before brackets
Then the debugger steps inside the brackets and the return MyArray[i]
is executed however I do not see Inside brackets
in the output by the time I have stepped through to the return MyArray[i]
line.
This behaviour is obviously (to me) wrong and I wondered if anyone else had encountered someth开发者_开发问答ing like this.
I am on a Windows XP, 64bit machine , with VS10 and .Net4.0.
Moon
ADDITIONAL1
I have been asked by Henk to provide a "console App" which I have done below. HOWEVER, as I would suspect, it does NOT SHOW the issue. I believe there is something else (threads?) causing me an in issue in my real app - and I obviously can post that. I realise I am not giving you a clearly defined problem - if I could, I would. That is why I am asking this question - incase it strikes a cord with somebody having something similar. For good order, here is console version which DOES NOT exhibit the problem. I have a feeling putting it on here will add to the confusion...
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;
}
}
}
}
ADDITIONAL 2
It seems that my issue is NOT one of the execution path going a wrong route ONLY VS debugger. i.e. It appears as if I am going inside the brackets, because debugger steps on the last statement, but the results I actually get appear correct.
I have managed to resolve the issue after a tip I found from a google web search. It seems I was running the debugger in "Optmise Code"
mode. This is something I checked early on, BUT what I didn't spot wad that although I had this option deselected for Debug Mode - I had "Release" selected in the Standard Toolbar. When I switched to "Debug
" the issue went away.
I must be missing something, but this is quite logic ?
- i is set to zero.
- i is not less than zero.
- Inside brackets is NOT outputted.
This is logical, isn't it ? Step through the code using the debugger. You'll see it makes sense.
Are you sure your if statement isn't as follows:
if (i < 0)
Console.WriteLine(...);
return MyArray(i);
Note the misleading indentation and the lack of brackets.
If i = 0
then i < 0
is false. If you set your condition to i <= 0
, then it executes the condition.
精彩评论