C#难题:可到达的goto指向不可达的标签 [英] C# Puzzle : Reachable goto pointing to an unreachable label
问题描述
以下是这篇文章中的Eric Lippert的评论:
Here's Eric Lippert's comment from this post:
现在您知道答案了,您可以 解决这个难题:给我写一个程序 在其中有一个可到达的goto 这将导致无法访问的标签. – 埃里克·利珀特(Eric Lippert)7月17日下午7:17
Now that you know the answer, you can solve this puzzle: write me a program in which there is a reachable goto which goes to an unreachable label. – Eric Lippert Jul 17 at 7:17
我无法创建将具有可到达的goto指向不可到达的标签的代码.那有可能吗?如果是,那么C#代码是什么样的?
I am not able to create a code which will have reachable goto pointing to an unreachable label. Is that even possible? If yes, what would the C# code look like?
注意:我们不要讨论'goto'的坏处,等等.这是理论上的练习.
Note: Let's not get into discussion about how 'goto' is bad etc. This is a theoretical exercise.
推荐答案
我的原始答案:
try
{
goto ILikeCheese;
}
finally
{
throw new InvalidOperationException("You only have cottage cheese.");
}
ILikeCheese:
Console.WriteLine("MMM. Cheese is yummy.");
这里没有编译器警告.
bool jumping = false;
try
{
if (DateTime.Now < DateTime.MaxValue)
{
jumping = (Environment.NewLine != "\t");
goto ILikeCheese;
}
return;
}
finally
{
if (jumping)
throw new InvalidOperationException("You only have cottage cheese.");
}
ILikeCheese:
Console.WriteLine("MMM. Cheese is yummy.");
这篇关于C#难题:可到达的goto指向不可达的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!