- Home /
Are c# methods arguments stored as local variables?
I'd like to know something about how are method arguments handled in Unity's c#. Suppose you have a method named myMethod ( int arg1 int arg2 int arg3 int arg4 int arg5)
. Suppose further that you're calling myMethod
within your script with arguments whose value are not known at compiletime, because the arguments are themselves calls to other methods that perform complicated calculations based on the game state.
Now, I imagine that the compiler might treat this situation in one of a number of ways, and I'd like to know how exactly.
One possibility is this: when the method is invoked with a set of provided arguments, the arguments are evaluated immediately and stored as local variables. Thus, arg1, arg2, arg3, arg4 and arg5 are all cmputed once. Later, in the body of the method, whenever I refer to one of the arguments, I'll be referring to the already computed value. That would also mean that if the body of the method have several conditional branches, each one mentioning only one of the arguments, then the arguments that would not have been used in the actual execution of the method body would have been computed for nothing.
On the other hand, this is another possibility: the arguments are NOT computed and stored immediately when the method is invoked. Instead they are computed each time that are actually "used" insde the method body. This would mean that there would be no innecessary computations in case some of the arguments are only used in conditional branches that don't get to be executed in a certain run of the game, but would also mean that if I plan to use the arguments repeatedly through the method body, I should save the arguments value to variables in the method body.
I figured I'd ask this here in case this depends on how C# is implemented for Unity (i.e. in differences between Mono and .NET, or between Unity's version of Mono and the "standard" more updated version of it. Also I didn't find any word about this neither in the .NET documentation nor through StackOverflow's questions.