What is the C # execution order in a function call with ref?

So, I just caught this error in our code:

class A { public int a; } var x = new A(); xa = 1; A qwe(ref A t) { t = new A(); ta = 2; return t; } void asd(A m, A n) { Console.WriteLine(ma); Console.WriteLine(na); } asd(x, qwe(ref x)); asd(x, qwe(ref x)); 

Is the execution order in the function call specified with respect to the order of the parameters?

What is written here:

 1 2 2 2 

This means that the first parameter reference is saved until the second parameter function is called.

Is this a specific behavior? I could not find specific information about the execution order in the C # lang spec.

+5
source share
1 answer

C # requires that parameter expressions passed to methods be evaluated from left to right.

Although qwe quits before calling asd , C # launched a link to "old" A before calling qwe in preparation for the call. This is why the first argument of the first call to asd receives the object "old" A before it is replaced with the new A object inside the qwe call.

+8
source

Source: https://habr.com/ru/post/1262571/


All Articles