在C#.NET中,我们来看下面的例子
[WebMethod]
public int TakeAction()
{
try {
//Call method A
Return 1;
} catch (Exception e) {
//Call method B
Return 0;
} finally {
//Call method C
}
}
现在假设方法 C 是一个长时间运行的过程。
调用 TakeAction 的客户端是否取回返回值,是在调用方法 C 之前,还是在调用/完成之后?
最佳答案
首先评估返回值,然后执行 finally block ,然后将控制权传递回调用方(带有返回值)。如果返回值的表达式将被 finally block 更改,则此顺序很重要。例如:
Console.WriteLine(Foo()); // This prints 10
...
static int Foo()
{
int x = 10;
try
{
return x;
}
finally
{
// This executes, but doesn't change the return value
x = 20;
// This executes before 10 is written to the console
// by the caller.
Console.WriteLine("Before Foo returns");
}
}
关于C# Web 服务 - 然后返回 最后 - 先发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19552618/