What's the standard behaviour for an out parameter when a TryXxxx method returns false?

Posted by Matt Lacey on Stack Overflow See other posts from Stack Overflow or by Matt Lacey
Published on 2010-03-15T11:08:35Z Indexed on 2010/03/15 11:09 UTC
Read the original article Hit count: 236

Assuming a method with the following signature

bool TryXxxx(object something, out int toReturn)

What is it acceptable for toReturn to be if TryXxxx returns false?

In that it's infered that toReturn should never be used if TryXxxx fails does it matter?

If toReturn was a nulable type, then it would make sense to return null. But int isn't nullable and I don't want to have to force it to be.

If toReturn is always a certain value if TryXxxx fails we risk having the position where 2 values could be considered to indicate the same thing. I can see this leading to potential possible confusion if the 'default' value was returned as a valid response (when TryXxxx returns true).

From an implementation point if view it looks like having toReturn be a[ny] value is easiest, but is there anything more important to consider?

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about c#