相关疑难解决方法(0)

编程语言中的协方差和逆变

谁能解释我,编程语言理论中协方差和逆变的概念?

c# c++ java programming-languages covariance

20
推荐指数
5
解决办法
5758
查看次数

为什么这"最终"执行?

如果你运行下面的代码,它实际上在每次调用goto后执行finally:

    int i = 0;
Found:
    i++;
    try
    {
        throw new Exception();
    }
    catch (Exception)
    {
        goto Found;
    }
    finally
    {
        Console.Write("{0}\t", i);
    }
Run Code Online (Sandbox Code Playgroud)

为什么?

c# goto exception try-catch-finally

17
推荐指数
3
解决办法
2096
查看次数

它是唯一一种指向无法访问的标签的可访问goto吗?

灵感来自问题

在我提出这个问题之前,我读过:

问题的挑战是

编写一个程序,该程序具有可访问的goto语句,但相应的标记语句无法访问 - Eric Lippert

一个可行的答案就像

    // the 3 lines are not important but declare variable for afterwards use
    var whateverException=new Exception("whatever exception");
    var whateverAction=default(Action);
    whateverAction=() => whateverAction();
Run Code Online (Sandbox Code Playgroud)
    try {
        goto whateverLabel; // (1) the goto is reachable
    }
    finally {
        throw whateverException; // (3) because finally hijacks
    }

whateverLabel: // (2) but the label is not really reached
    whateverAction();
Run Code Online (Sandbox Code Playgroud)

我想知道在一个单线程程序中,它是唯一一个指向无法访问标签的可达goto吗?以下代码也被认为是可行的答案吗?

here:
    int d=0, n=1/d;
    goto here;
Run Code Online (Sandbox Code Playgroud)

c# unreachable-code

1
推荐指数
1
解决办法
242
查看次数