What’s the difference between parameters declared with var and those declared with out? How does the compiler treat them differently (e.g., by generating different code, or by changing which diagnostics it issues)? Or do the different modifiers merely allow the programmer to document intended use of the parameters? What effect do the types of the parameters have on the matter?
What’s the difference between parameters declared with var and those declared with out ?
Share
A
varparameter will be passed by reference, and that’s it.An
outparameter is also passed by reference, but it’s assumed that the input value is irrelevant. For managed types, (strings, Interfaces, etc,) the compiler will enforce this, by clearing the variable before the routine begins, equivalent to writingparam := nil. For unmanaged types, the compiler implementsoutidentically tovar.Note that the clearing of a managed parameter is performed at the call-site and so the code generated for the function does not vary with
outorvarparameters.