When is "Try" supposed to be used in C# method names?
This is known as the TryParse pattern and has been documented by Microsoft. The official Exceptions and Performance MSDN page says:
Consider the TryParse pattern for members that may throw exceptions in common scenarios to avoid performance problems related to exceptions.
Thus if you have code for which a regular use case would mean that it might throw an exception (such as parsing an int), the TryParse pattern makes sense.
(Corrected) There is official guideline, as Erik suggested.
When I see TrySomething
method, I assume it
- doesn't throw
- returns
bool
- if I expect value, it is returned via 'out' parameter
- there exists
Something
method, that allows me to handle any exception myself. (edit, suggested by Jesse Webb)
I think you should use try
when you want to proceed. It doesn't matter that a method returns some value or not.
Case 1: if it returns fine, you can proceed in some way.
Case 2: if it does not return: it is still fine; you can proceed in some other way.
And if you expect some value as output of that method then use the out
parameter.
Example
int value
if (dictionary.TryGetValue("key", out value))
{
// Proceed in some way
}
else
{
// Proceed in some other way
}