I despise out’s and ref’s as parameters on methods. IMHO, they make code less clean and provide opportunities for side-effects. But I concede that I may not understand their usefulness which might explain part of my loathing. Please, can someone explain a valid case for out’s or ref’s?
Share
Basically if you need to return more than one value, it’s an alternative to using something like
Tuple<,>or a custom type to encapsulate the values. The canonical example is probablyint.TryParseand related methods. They want to convey two pieces of information back:Now these could actually have been written using a return type of
int?etc in this case, but it’s the same principle for other cases. (For example,Dictionary<,>.TryGetValue, where the value stored in the dictionary may legitimately be null.)I wouldn’t say I despise
outandrefparameters, but I do believe they should only be used occasionally, and only when there isn’t a better alternative. Most of the uses ofrefI see on Stack Overflow are due to a misunderstanding of parameter passing.