Skip to content

Commit 886befc

Browse files
committed
Added Parse extensions
1 parent b4023c2 commit 886befc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace ZirconNet.Core.Extensions;
9+
10+
#if NET7_0_OR_GREATER
11+
public static class ParsableExtensions
12+
{
13+
public static T Parse<T>(this string input, IFormatProvider? formatProvider = null)
14+
where T : IParsable<T>
15+
{
16+
return T.Parse(input, formatProvider);
17+
}
18+
19+
20+
public static T Parse<T>(this ReadOnlySpan<char> input, IFormatProvider? formatProvider = null)
21+
where T : ISpanParsable<T>
22+
{
23+
return T.Parse(input, formatProvider);
24+
}
25+
26+
public static bool TryParse<T>(this string input, IFormatProvider? formatProvider, [MaybeNullWhen(false)] out T result)
27+
where T : IParsable<T>
28+
{
29+
return T.TryParse(input, formatProvider, out result);
30+
}
31+
32+
33+
public static bool TryParse<T>(this ReadOnlySpan<char> input, IFormatProvider? formatProvider, [MaybeNullWhen(false)] out T result)
34+
where T : ISpanParsable<T>
35+
{
36+
return T.TryParse(input, formatProvider, out result);
37+
}
38+
}
39+
#endif

0 commit comments

Comments
 (0)