C
Chris Mayers
'out' and 'ref' parameters in C#...
Both these can be used to pass parameter values BACK from a Method, but
obviously they are different techniques.
As I understand it,
'ref' parameter passes the object into the Method by Reference, ie passes
the memory location of the original object in the parameter list. The Method
may change the value of the object and by doing so, will be changing the
value of the original object.
'out' parameter passes an object BY VALUE back into the object in the
calling parameters.
1) Am I right?
2) Is it true to say that you are usually better to use 'out' unless the
original value of the parameter being passed in effects its value when you
pass it back. Or if your method needs to return more than one calculated
result.
eg
private void GetPosition(string lorryId, out int xLocation, out int
yLocation)
Hope this makes sense, I'm very tired...
Thanks,
Chris.
Both these can be used to pass parameter values BACK from a Method, but
obviously they are different techniques.
As I understand it,
'ref' parameter passes the object into the Method by Reference, ie passes
the memory location of the original object in the parameter list. The Method
may change the value of the object and by doing so, will be changing the
value of the original object.
'out' parameter passes an object BY VALUE back into the object in the
calling parameters.
1) Am I right?
2) Is it true to say that you are usually better to use 'out' unless the
original value of the parameter being passed in effects its value when you
pass it back. Or if your method needs to return more than one calculated
result.
eg
private void GetPosition(string lorryId, out int xLocation, out int
yLocation)
Hope this makes sense, I'm very tired...
Thanks,
Chris.