本文介绍了C#难题:可到达的goto指向不可达的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是这篇文章中的Eric Lippert的评论:

Here's Eric Lippert's comment from this post:

我无法创建将具有可到达的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指向不可达的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 08:21