r/ProgrammerTIL • u/insulind • Oct 17 '17
C# [c#] TIL method variables are captured left to right always, not first evaluating method calls that are returning values to be used as arguments.
EDIT: As pointed out by many correct people this is not good practice, it is infact very bad practice. I am aware of this, this was never something I was doing to put out into the world it was just a mess around. This example just highlights quite well that method calls as arguments are not evaluated first, it always just left to right as /u/Yare_Owns said, "the comma operator has left to right associativity".
It's a bit niche but it caught me out. I assumed method calls would be evaluated first eg.
https://www.pastebucket.com/564283
The value printed out is the initial value. If you make the call to myRefMethod in a separate line before the call to MyMethod, you'll see that the myString variable is change as it's passed by reference and it prints out "new value".
But method arguments are captured left to right always, unlike brackets in an equation where you work inside out. Maybe this was obvious to everyone else but not me
Edit: some code that will compile thanks to /u/blackstarsolar
https://dotnetfiddle.net/UoIKjR
https://dotnetfiddle.net/04uRkl - this has the call to the ref method on a separate line to show the difference
https://dotnetfiddle.net/cfDLWg - this one really highlights what I am trying to get across
11
u/HighRelevancy Oct 18 '17
I feel like depending on this sort of behaviour is a terrible idea, and if you're writing code where this information is important you should probably not be writing it that way.
5
u/BlackstarSolar Oct 17 '17
Interesting, I have to admit I'd never thought about this but now I realised this is how I always assumed it worked.
Your code doesn't compile so I've fixed that for you. Sorry for the formatting I'm on mobile.
Here's a cleaned up working version https://dotnetfiddle.net/UoIKjR And here's a version with the call to MyRefMethod on a separate line to show the difference https://dotnetfiddle.net/04uRkl
p.s. avoid ref and out :)
2
u/forsubbingonly Oct 17 '17
If you reorder the args does the passed in string change?
2
u/[deleted] Oct 17 '17
I assume that if you actually needed the other string, the inner method would get its time to run first?
2
u/ForkForkFork Oct 18 '17
Aren't there really two things at play here? Yes, C# comma-separated lists are left-to-right associative, as has been pointed out. But, arguments are also evaluated lazily in C#. Because of the guarantee that
MyRefMethodwill return a String, it's value isn't calculated until you actually need it. Before such time, it's just a String-typed reference to a function with applied arguments.The reason switching the argument order matters, is because you make your method call a used part of the code. It actually needs to yield a value. The fact that it is first in line is a red-herring.
As has also been pointed out, code that relies on side-effects in the way you have exampled is likely better off rewritten into something more explicit and less devious (for like 100 reasons).
2
u/[deleted] Oct 18 '17
Comma operator has left-to-right associativity. You can use them anywhere you want to force a sequence point -not just in argument lists.
https://en.wikipedia.org/wiki/Sequence_point
Also you probably want to think about the structure of your code if the side effects of one argument are affecting another argument. Methods with fewer side effects are less prone to bugs.
https://en.wikipedia.org/wiki/Side_effect_(computer_science)