C#Web服务 - 返回然后最后 - 首先发生什么

ada*_*dam 3 .net c# asp.net web-services try-finally

在C#.NET中,我们来看以下示例

[WebMethod]
public int TakeAction()
{
    try {
        //Call method A
        Return 1;
    } catch (Exception e) {
        //Call method B
        Return 0;
    } finally {
        //Call method C
    }
}
Run Code Online (Sandbox Code Playgroud)

现在让我们说方法C是一个长期运行的过程.

在调用方法C之前,或在调用/完成方法之后,调用TakeAction的客户端是否返回返回值?

Jon*_*eet 10

返回值是评价首先,则最后块执行,则控制被传递回给调用者(与返回值).如果finally块将更改返回值的表达式,则此排序很重要.例如:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)