I am trying to formalize the use of the "out" keyword in C # for the project I am working on, especially with respect to any public methods. It seems that I can’t find the best practices and would like to know what is good or bad.
Sometimes I see some method signatures that look like this:
public decimal CalcSomething(Date start, Date end, out int someOtherNumber){}
At this moment, it’s just a feeling, it’s not very good with me. For some reason, I would rather see:
public Result CalcSomething(Date start, Date end){}
where the result is a type that contains a decimal number and some other number. I think it makes reading easier. This allows you to extend the result or add properties without breaking the code. It also means that the caller of this method does not need to declare a local cloud "someOtherNumber" before calling. From expectations of use, not all subscribers will be interested in "someOtherNumber".
In contrast, the only instances I can present right now in the framework of .Net where the "out" parameters make sense are methods like TryParse (). This actually forces the caller to write a simpler code in which the caller will be primarily interested in the out parameter.
int i; if(int.TryParse("1", i)){ DoSomething(i); }
I think that the "exit" should be used only if the return type is bool, and the expected conditions of use are where the "out" parameters will always interest the caller, by design.
Thoughts?
user1010 Jan 05 '09 at 14:16 2009-01-05 14:16
source share