调试期间的奇怪行为 [英] Strange behaviour during debugging
问题描述
嗨专家,
调试此代码时:
Hi experts,
When debugging this code:
enum MyEnum
{
firstEnumEntry,
secondEnumEntry
}
MyEnum _enumVariable = MyEnum.firstEnumEntry;
private void MyMethod()
{
switch (_enumVariable)
{
case MyEnum.firstEnumEntry:
_enumVariable = MyEnum.secondEnumEntry;
MyMethod();
break;
case MyEnum.secondEnumEntry:
DoSomeOtherThing();
break;
default:
DoSomeDefaultThing();
break;
}
}
void DoSomething() { }
void DoSomeOtherThing() { }
void DoSomeDefaultThing() { }
void DoCoolStuff() { }
我遇到执行步入 case firstEnumEntry
并运行 MyMethod的行为()
第二次。当它到 switch()
时,它退出 MyMethod()
。没有例外,没有突出显示的代码行。程序只是继续,好像我已经击中了F5,除了方法的其余部分没有执行。
这里发生了什么?
我是否在尝试
块中包装一个简单的 switch()
?那里什么都不应该出错。
Ciao,
lukeer
根据Mahesh Bailwal的建议,我使用了很多断点。假设触发了 DoCoolStuff()
前面的那个。该方法被调用并工作但是当我期望执行返回时,同样的事情发生如上所述。
DoCoolStuff()
的结束括号是未突出显示且执行未返回 MyMethod()
。相反,程序继续就好像有人击中了F5。
[/ Edit]
I came across the behaviour that execution stepped into case firstEnumEntry
and run MyMethod()
for the second time. When it got to switch()
, it quit MyMethod()
. No exception, no highlighted code line. The program just continued as if I had hit F5, except that the rest of the method did not execute.
What has happened here?
Am I to wrap a simple switch()
in a try
block? There's nothing in there that should be able to go wrong.
Ciao,
lukeer
According to Mahesh Bailwal's suggestion, I used a lot of breakpoints. Let's say the one in front of DoCoolStuff()
was triggered. The method was called and worked but when I expected execution to return, same thing happened as described above.
DoCoolStuff()
's closing bracket was not highlighted and execution didn't return to MyMethod()
. Instead the program continued just as if someone had hit F5.
[/Edit]
推荐答案
我按原样尝试了你的示例代码(在每个方法中使用Console.WriteLine)并从Button Click处理程序调用它:
I tried your example code above as is (with Console.WriteLine in each method) and called it from a Button Click handler:
private void button1_Click(object sender, EventArgs e)
{
MyMethod();
}
enum MyEnum
{
firstEnumEntry,
secondEnumEntry
}
MyEnum _enumVariable = MyEnum.firstEnumEntry;
private void MyMethod()
{
switch (_enumVariable)
{
case MyEnum.firstEnumEntry:
_enumVariable = MyEnum.secondEnumEntry;
MyMethod();
break;
case MyEnum.secondEnumEntry:
DoSomeOtherThing();
break;
default:
DoSomeDefaultThing();
break;
}
}
void DoSomething()
{
Console.WriteLine("void DoSomething()");
}
void DoSomeOtherThing()
{
Console.WriteLine("void DoSomeOtherThing()");
}
void DoSomeDefaultThing()
{
Console.WriteLine("void DoSomeDefaultThing()");
}
void DoCoolStuff()
{
Console.WriteLine("void DoCoolStuff()");
}
没有断点跑:打印
Ran without breakpoints: printed
void DoSomeOtherThing()
再次跑,在开关上有一个断点,并在每一行步入:
打破了确定
步入firstEnumEntry
步入MyMethod
步入secondEnumEntry
步入DoSomeOtherThing
打印void DoSomeOtherThing()
退出MyMethod,回到firstEnumEntry break
声明。
退出MyMethod
这就是我所期待的。
基本上,我根本没有得到任何奇怪的效果:你做的和我有什么不同?它可能是你的DoThis方法的内容吗?
Ran again, with a breakpoint on the switch, and "step into" on each line:
Broke ok
Stepped into "firstEnumEntry"
Stepped into "MyMethod"
Stepped into "secondEnumEntry"
Stepped into "DoSomeOtherThing"
Printed "void DoSomeOtherThing()"
Stepped out of "MyMethod", back to "firstEnumEntry" break
statement.
Stepped out of "MyMethod"
Which is what I would expect.
Basically, I didn't get any odd effects at all: what are you doing that is different from me? Could it be the contents of your "DoThis" methods?
这篇关于调试期间的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!