Less Verbose TryParse #80962
Replies: 3 comments 1 reply
-
If you're proposing a new extension method on |
Beta Was this translation helpful? Give feedback.
-
You can write your own extension method to include every public static bool TryParse<T>(this string s, [MaybeNullWhen(false)] out T f)
where T : System.ISpanParsable<T>
=> T.TryParse(s, null, out f); And use it like that if ("3.141592".TryParse(out double pi))
Console.WriteLine($"pi = {pi}"); |
Beta Was this translation helpful? Give feedback.
-
Maybe it's not just me but I happen to have such publicly available extensions. The generic |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
A less verbose
TryParse
method, parsing directly from the string object into its relative typeCurrent Implementation
Fig 1.1
Proposed Implementation
Fig 1.2
Workaround
Fig 1.3
Motivation
Our team regularly uses variations of the
TryParse
extension method (Fig 1.3) for various string operations.We have been using (with no observable overhead), and an improvement to workflow of up to 10 seconds per usage.
We feel that the entire C# community could benefit from this improvement.
Setback
The
TryParse
method has been used in its current state in several revisions of the language. This new change would require users to alter their workflow when usingTryParse
methods.As such, it is also proposed that a larger number of up votes than the usual be required for this new change to be adopted, should this proposal be accepted as a candidate.
Beta Was this translation helpful? Give feedback.
All reactions