C#拼图:可达到goto指向无法访问的标签(C# Puzzle : Reachable goto pointing to an unreachable label)

编程入门 行业动态 更新时间:2024-10-09 11:23:52
C#拼图:可达到goto指向无法访问的标签(C# Puzzle : Reachable goto pointing to an unreachable label)

这里是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.");

更多推荐

本文发布于:2023-08-07 17:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拼图   无法访问   标签   可达到   goto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!