SpanExtensionsParseT(ReadOnlySpanChar, CultureInfo) Method
Parses an object of type
T from a
String represented by a
ReadOnlySpan<char> value.
Firstly, it tries to parse the type natively. If
T cannot be parsed natively but the type has a
TypeConverter
or a registered conversion that can convert from string, then the type converter or conversion will be used.
Namespace: KGySoft.CoreLibrariesAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 10.0.0
public static T Parse<T>(
this ReadOnlySpan<char> s,
CultureInfo? culture = null
)
<ExtensionAttribute>
Public Shared Function Parse(Of T) (
s As ReadOnlySpan(Of Char),
Optional culture As CultureInfo = Nothing
) As T
public:
[ExtensionAttribute]
generic<typename T>
static T Parse(
ReadOnlySpan<wchar_t> s,
CultureInfo^ culture = nullptr
)
[<ExtensionAttribute>]
static member Parse :
s : ReadOnlySpan<char> *
?culture : CultureInfo
(* Defaults:
let _culture = defaultArg culture null
*)
-> 'T
- s ReadOnlySpanChar
- The ReadOnlySpan<char> value to parse. If represents and T is a reference or nullable type, then the method returns .
- culture CultureInfo (Optional)
- The culture to use for the parsing. If , then the InvariantCulture will be used. This parameter is optional.
Default value: .
- T
- The desired type of the return value.
TAn instance of
T, which is the result of the parsing. A
reference can be returned if
s represents
, and
T is a reference or nullable type.In Visual Basic and C#, you can call this method as an instance method on any object of type
ReadOnlySpanChar. When you use instance method syntax to call this method, omit the first parameter. For more information, see
Extension Methods (Visual Basic) or
Extension Methods (C# Programming Guide).
The following types are parsed natively:
Apart from
Enum and
Type types, no string allocation occurs when parsing any of the types above.
When targeting at least .NET 6.0, no string allocation occurs even for
Enum types.
New conversions can be registered by the RegisterConversion extension methods.
If a registered conversion can convert from string, then it can be used, though in that case a string allocation will occur.
A TypeConverter can be registered by the RegisterTypeConverter extension method.
If a type converter can convert from string, then it can be used, though in that case a string allocation will occur.