Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created October 17, 2022 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ufcpp/33438c5c12dbe7a00b9dfb5f582673b4 to your computer and use it in GitHub Desktop.
Save ufcpp/33438c5c12dbe7a00b9dfb5f582673b4 to your computer and use it in GitHub Desktop.
ParseOrNull
Console.WriteLine("123".ParseOrNull<int>());
Console.WriteLine("1a3".ParseOrNull<int>());
static class Ex
{
public static T? ParseOrNull<T>(this string? s, IFormatProvider? provider = null)
where T : struct, IParsable<T>
=> T.TryParse(s, provider, out var value) ? value : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment