这里是Eric Lippert从这篇文章发表的评论:
现在你知道答案了,你可以解决这个难题:给我写一个程序,其中有一个可到达的goto,它转到一个无法访问的标签。 - Eric Lippert 7月17日7:17
我无法创建一个代码,它可能会指向无法访问的标签。 这甚至有可能吗? 如果是的话,那么C#代码会是什么样子?
注意:我们不要讨论'goto'如何坏等。这是一个理论练习。
Here's Eric Lippert's comment from this post:
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
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?
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.");My original answer:
try { goto ILikeCheese; } finally { throw new InvalidOperationException("You only have cottage cheese."); } ILikeCheese: Console.WriteLine("MMM. Cheese is yummy.");Here is without the compiler warning.
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.");更多推荐
发布评论