Home > Net >  Problems about.net technology
Problems about.net technology

Time:10-01

Console. WriteLine (getb (1, 2));
Static int getb (int a, int b)
{
Try {
B=a + b;
Console. WriteLine (b);
return b;
}
The finally {
B=3 * b;
Console. WriteLine (b);
}
}

Everyone a great god passing, help please, thank you!

CodePudding user response:

Finally whether success or abnormal, will perform again

CodePudding user response:

In c #, the finally part, can be carried in when you leave the try block,
So, when you return to 3 the try block, finally is in charge of the implementation of a code block, and print the 9,

CodePudding user response:

What is the problem?
Finally will be executed before the return,

CodePudding user response:

Good job

CodePudding user response:

What is the problem??

CodePudding user response:

3
9
9

CodePudding user response:

The problem is the output

CodePudding user response:

To perform the try the content inside the
Output a + b, that is, 3
Before the return to perform the finally content
The output 3 * b, which is 9
Final output the result of the return, that is, a + b

The final output results: 3 September 3

CodePudding user response:

cited the 6th floor in the autumn red fruit response:
3
9
9

I put in the VS run is 393

CodePudding user response:

references 9 f OrdinaryCoder response:
Quote: refer to the 6th floor in the autumn red fruit response:

3
9
9

I run on the VS 393
? B the scope is the function of body, has turned into 9, how could you or 3?

CodePudding user response:

Oh, no, no discussion, XX garden wind is up,

As can understand execution order, make some logo, see,

But the wind of "back to" not up, if I were in coderview, see this god code (including + + i++ - this class) I would ask you to come over a cup of tea, would you please explain what the hell are you irreplaceable case god wrote this code, if can't explain, I'm sorry, buckle 50 pieces, meeting on Monday morning

CodePudding user response:

references to the tenth floor autumn red fruit reply:
Quote: references 9 f OrdinaryCoder response:

Quote: refer to the 6th floor in the autumn red fruit response:

3
9
9

I run on the VS 393
? B the scope is the function of body, has turned into 9, how could you or 3?

I put into VS run once it is

CodePudding user response:

The finally performed anyway

CodePudding user response:

3
9
3

CodePudding user response:

The signature, 393,

CodePudding user response:

Who taught you so write the code? !
To scold the man!
So the wicked!

CodePudding user response:

11 references wanghui0380 response:
ah, don't entanglements, don't discuss, XX garden wind is up,

As can understand execution order, make some logo, see,

But the wind of "back to" not up, if I were in coderview, see this god code (including + + i++ - this class) I would ask you to come over a cup of tea, would you please explain what the hell are you irreplaceable case god wrote this code, if can't explain, I'm sorry, buckle 50 pieces, see you Monday morning meeting

And goto

CodePudding user response:

One explanation is executed in the catch to return this sentence before, b=3 into the stack, and then execute the finally, when b=9 opened another temporary space, after when performing the finally, temporary space destruction, b program automatically call before preserved b=3 this stack space, the returned result is b=3, so the result is 393

CodePudding user response:

Website:
By using a finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block. Typically, the statements of a finally block, run The when control leaves a try statement . The transfer of control can occur as a result of normal execution, of The execution of a break, continue, goto, or The return statement, or of The propagation of an exception out of The try statement.

Thought for a few days also didn't understand the implementation process, the proof today?
Temporary interpreted as:
The finally block is cleaning the scene of the
Is to return first, and then execute the finally, see below testing,
Why first output in the finally b (9), and then output function getb return value?
Because getb is a whole, the function should perform the (performed finally) to output the return value

The test code
 
Static int getb (int a, int b)
{
Try
{
B=a + b;
Console. WriteLine (b.T oString () + "in the try");
Console. WriteLine (DateTime. Now);

return b;
}
The finally
{
Thread.Sleep(1000);
Console. WriteLine (DateTime. Now);

B=3 * b;
Console. WriteLine (b.T oString () + "in the finally");
}
}



Results:


Output to the 17:36:54 suspended 1 second, just the output of the 55

Official explained clearly: when leaving the try block, when to launch the finally block

CodePudding user response:

I do not understand the c #, it will only vb.net, but the principle is the same, basically should be 393

CodePudding user response:

 
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related