Quiz: who win in finally vs return?

Source: Internet
Author: User

I will not talk much about it. Let's take a look at the example below.
1) Static int test ()
{
Int val = 1;
Try
{
Return val;
}
Finally
{
Val = 2;
}
}
2) Static Int S_val;
Static Int Test ()
{
S_val = 1;
Try
{
Return S_val;
}
Finally
{
S_val = 2;
}
}
What is the final return value of the two examples?
Let's take a closer look at these two examples and we will know 2) the difference with 1) is that the former uses static variables instead of local variables. So what are the differences between them?
The answer is that their return values are the same, all of which are 1.
For ease of explanation, let's take out their Il Code Let's see:

Test () Il
1 . Method Private Hidebysig Static Int32 test () cel managed
2 {
3 // Code size 16 (0x10)
4 . Maxstack 1
5 . Locals Init ([ 0 ] Int32 Val,
6 [ 1 ] Int32 CS $ 1 $ 0000 )
7 Il_0000: NOP
8 Il_0001: LDC. i4. 1 // int val = 1
9 Il_0002: stloc. 0
10 . Try
11 {
12Il_0003: NOP
13Il_0004: ldloc.0 // variable 0 (VAL) into Stack
14Il_0005:Stloc.1 // here an intermediate Hidden variable 1 ($ RET) is created to return the value (VAL) at the top of the stack ).
15Il_0006: Leave. s il_000d
16} // End. Try
17 Finally
18 {
19 Il_0008: NOP
20 Il_0009: LDC. i4. 2 // int val = 2 merged into Stack
21 Il_000a: stloc. 0 // return the value at the top of the stack to the variable 0 (VAL)
22 Il_000b: NOP
23 Il_000c: endfinally
24 } // End Handler
25 Il_000d: NOP
26 Il_000e:Ldloc.1 // retrieve the variable 1 ($ RET). This is the return value, but here we can see that the Val value has changed.
27 Il_000f: Ret
28 }   // End of method program: Test
29 Test1 () Il
1 . Method Private Hidebysig Static Int32 test1 () cel managed
2 {
3 // Code size 28 (0x1c)
4 . Maxstack 1
5 . Locals Init ([ 0 ] Int32 CS $ 1 $ 0000 )
6 Il_0000: NOP
7 Il_0001: LDC. i4. 1 // value 1 is added to the stack.
8 Il_0002: sts1_int32 leleapp. Program: s_val // pass the stack top value to the static variable
9 . Try
10 {
11Il_0007: NOP
12Il_0008: lds1_int32 consoleapp. Program: s_val // retrieve static variables
13Il_000d: stloc.0 // The Hidden variable from the top of the stack that is passed to the local variable 0 (here the static variable is not the local variable 0)
14Il_000e: Leave. s il_0019
15} // End. Try
16 Finally
17 {< br> 18 il_0010: NOP
19 il_0011: LDC. i4. 2 // same as above
20 il_0012: sts0000int32 consoleapp. program: s_val // The storage static variable is 2
21 il_0017: NOP
22 il_0018: endfinally
23 } // End Handler
24 Il_0019: NOP
25 Il_001a: ldloc. 0 // obtain the value of local variable 0 as the return value.
26 Il_001b: Ret
27 }   // End of method program: test1
28

Both of these methods generate a Hidden variable to store the returned value, instead of directly returning the variable address.
If you know more about the working principles of CLR, it should not be difficult to understand. CLR allocates local variables and return values of methods to a thread stack. A method starts from its real parameters in the thread stack and ends with the return value. Therefore, the return value has its own storage address. In this way, the above problem can be explained, which looks like {return I ++ ;}

You are welcome to make a brick.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.