谁能解释我,编程语言理论中协方差和逆变的概念?
如果你运行下面的代码,它实际上在每次调用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)
为什么?
灵感来自问题
在我提出这个问题之前,我读过:
问题的挑战是
编写一个程序,该程序具有可访问的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)