Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer TryParse instead of Parse to circumvent exception handling #100

Closed
JamesNZL opened this issue Jul 6, 2023 · 0 comments
Closed

Prefer TryParse instead of Parse to circumvent exception handling #100

JamesNZL opened this issue Jul 6, 2023 · 0 comments
Assignees
Labels
performance Improvements to performance

Comments

@JamesNZL
Copy link
Owner

JamesNZL commented Jul 6, 2023

https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parse?view=net-7.0#the-string-to-parse

If the parsing operation fails because of an unrecognized string format, the Parse method throws a FormatException, whereas the TryParse method returns false. Because exception handling can be expensive, you should use Parse when the parsing operation is expected to succeed because the input source is trusted. TryParse is preferable when parsing failures are likely, particularly because an input source is not trusted, or you have reasonable default values to substitute for strings that do not parse successfully.

@JamesNZL JamesNZL added the performance Improvements to performance label Jul 6, 2023
@JamesNZL JamesNZL self-assigned this Jul 6, 2023
@JamesNZL JamesNZL closed this as completed Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Improvements to performance
Projects
None yet
Development

No branches or pull requests

1 participant