The output is good if you have a TryNNN function, and it is clear that the out parameter will always be set, even if the function fails. This allows you to rely on the fact that the local variable you are declaring will be set instead of putting checks later in your code with a null value. (The comment below indicates that the parameter can be set to null , so you can check the documentation for the function you are calling to make sure it is or not.) This makes the code a little understandable and easier to read. Another case is when you need to return some data and status to the state of the method, for example:
public bool DoSomething(int arg1, out string result);
In this case, the return may indicate whether the function was successful, and the result is stored in the out parameter. Admittedly, this example is contrived because you can design a way where a function simply returns a string , but you get an idea.
The downside is that you have to declare a local variable in order to use them:
string result; if (DoSomething(5, out result)) UpdateWithResult(result);
Instead:
UpdateWithResult(DoSomething(5));
However, this may not even be a drawback, it depends on the design that you are going to use. In the case of DateTime, both tools (Parse and TryParse) are provided.
jasonh Jul 23 '09 at 5:43 2009-07-23 05:43
source share