As far as I know, int.TryParse(string, out int)
exists since Framework 2.0. So does int?
.
Is there a reason to use an out
parameter instead of returning an int?
with HasValue
set to true
of false
depending on the ability to convert ?
TryParse(String, Int32) Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.
The out parameter is how the TryParse method passes the parsed value back to you. It's a parameter of type T, where T is the type that you are trying to parse the string into.
TryParse(String, Single) Converts the string representation of a number to its single-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.
int. TryParse contain two arguments first is string and another is int(out type). If the input string is integer it returns 2nd arguments(out type int). Else it returns first argument(string).
I cannot tell about the actual reasons, but I see three possible reasons:
1) Nullable types were introduced in .NET 2.0, while the first TryParse
methods were already around since .NET 1.1. Thus, when nullable types were introduced, it was too late for such an API change; and new classes wouldn't implement TryParse
differently because the pattern had already been set.
2) Not all types can be used with the Nullable
structure, only value types can. However, there are methods following the Try*
pattern that have to return reference types. For example, a dictionary may totally legitimately contain null
as an item, hence its TryGetValue
method needs an additional way to express that a key was not found.
3) The way the Try*
-methods are written, it is possible to write code like this:
int myValue;
if (int.TryParse("42", out myValue)) {
// do something with myValue
}
// do something else
}
Now, imagine if TryParse
only returned an int?
. You can either dispose of the myValue
variable and lose the result:
if (int.TryParse("42").HasValue) {
// do something with ... what? You didn't store the conversion result!
}
// do something else
}
Or you can add a nullable variable:
int? myValue = int.TryParse("42");
if (myValue.HasValue) {
// do something with myValue.Value
}
// do something else
}
This isn't an advantage over the current version any more, and instead it requires writing myValue.Value
at some later instances, where otherwise a simple value
would have sufficed. Note that in many cases, you only need the information about whether the operation was successful for the if
statement.
The simple reason is because when int.TryParse
was added to the language, Nullable<T>
didn't exist.
In this blog post by Eric Lippert, there's a line towards the bottom that reads:
The solution is to write your own extension method version of TryParse the way it would have been written had there been nullable value types available in the first place
which makes it clear that nullable types were not available to be used in the original implementation of TryParse
. Eric Lippert was on the team that wrote the C# compiler, so I'd say that's a pretty authoritative source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With