Your suggestion of result = result
won't work, because it's an out
parameter - it's not definitely assigned to start with, so you can't read its value until you've assigned a value to it.
result = null;
is definitely the right way to go for an object
out parameter. Basically use default(T)
for whatever type T
you've got. (The default
operator is useful in generic methods - for non-generic code I'd normally just use null
, 0, whatever.)
EDIT: Based on the comment from Boris, it may be worth elaborating on the difference between a ref
parameter and an out
parameter:
Out parameters
- Don't have to be definitely assigned by the caller
- Are treated as "not definitely assigned" at the start of the method (you can't read the value without assigning it first, just like a local variable)
- Have to be definitely assigned (by the method) before the method terminates normally (i.e. before it returns; it can throw an exception without assigning a value to the parameter)
Ref parameters
- Do have to be definitely assigned by the caller
- Are treated as "definitely assigned" at the start of the method (so you can read the value without assigning it first)
- Don't have to be assigned to within the method (i.e. you can leave the parameter with its original value)