Boolean.TryParse Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
TryParse(ReadOnlySpan<Char>, Boolean) |
Tries to convert the specified span representation of a logical value to its Boolean equivalent. |
TryParse(String, Boolean) |
Tries to convert the specified string representation of a logical value to its Boolean equivalent. |
- Source:
- Boolean.cs
- Source:
- Boolean.cs
- Source:
- Boolean.cs
Tries to convert the specified span representation of a logical value to its Boolean equivalent.
public:
static bool TryParse(ReadOnlySpan<char> value, [Runtime::InteropServices::Out] bool % result);
public static bool TryParse (ReadOnlySpan<char> value, out bool result);
static member TryParse : ReadOnlySpan<char> * bool -> bool
Public Shared Function TryParse (value As ReadOnlySpan(Of Char), ByRef result As Boolean) As Boolean
Parameters
- value
- ReadOnlySpan<Char>
A span containing the characters representing the value to convert.
- result
- Boolean
When this method returns, if the conversion succeeded, contains true
if value
is equal to TrueString or false
if value
is equal to FalseString. If the conversion failed, contains false
. The conversion fails if value
is null
or is not equal to the value of either the TrueString or FalseString field.
Returns
true
if value
was converted successfully; otherwise, false
.
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Standard | 2.1 |
- Source:
- Boolean.cs
- Source:
- Boolean.cs
- Source:
- Boolean.cs
Tries to convert the specified string representation of a logical value to its Boolean equivalent.
public:
static bool TryParse(System::String ^ value, [Runtime::InteropServices::Out] bool % result);
public static bool TryParse (string value, out bool result);
public static bool TryParse (string? value, out bool result);
static member TryParse : string * bool -> bool
Public Shared Function TryParse (value As String, ByRef result As Boolean) As Boolean
Parameters
- value
- String
A string containing the value to convert.
- result
- Boolean
When this method returns, if the conversion succeeded, contains true
if value
is equal to TrueString or false
if value
is equal to FalseString. If the conversion failed, contains false
. The conversion fails if value
is null
or is not equal to the value of either the TrueString or FalseString field.
Returns
true
if value
was converted successfully; otherwise, false
.
Examples
The following example calls the TryParse method to parse an array of strings. Note that the parse operation succeeds only if the string to be parsed is "True" (the value of the TrueString field) or "False" (the value of the FalseString field) in a case-insensitive comparison.
using System;
public class Example
{
public static void Main()
{
string[] values = { null, String.Empty, "True", "False",
"true", "false", " true ", "0",
"1", "-1", "string" };
foreach (var value in values) {
bool flag;
if (Boolean.TryParse(value, out flag))
Console.WriteLine("'{0}' --> {1}", value, flag);
else
Console.WriteLine("Unable to parse '{0}'.",
value == null ? "<null>" : value);
}
}
}
// The example displays the following output:
// Unable to parse '<null>'.
// Unable to parse ''.
// 'True' --> True
// 'False' --> False
// 'true' --> True
// 'false' --> False
// ' true ' --> True
// Unable to parse '0'.
// Unable to parse '1'.
// Unable to parse '-1'.
// Unable to parse 'string'.
open System
let values =
[ null; String.Empty; "True"; "False"
"true"; "false"; " true "; "0"
"1"; "-1"; "string" ]
for value in values do
match Boolean.TryParse value with
| true, flag ->
printfn $"'{value}' --> {flag}"
| false, _ ->
printfn $"""Unable to parse '%s{if value = null then "<null>" else value}'."""
// The example displays the following output:
// Unable to parse '<null>'.
// Unable to parse ''.
// 'True' --> True
// 'False' --> False
// 'true' --> True
// 'false' --> False
// ' true ' --> True
// Unable to parse '0'.
// Unable to parse '1'.
// Unable to parse '-1'.
// Unable to parse 'string'.
Module Example
Public Sub Main()
Dim values() As String = { Nothing, String.Empty, "True", "False",
"true", "false", " true ", "0",
"1", "-1", "string" }
For Each value In values
Dim flag As Boolean
If Boolean.TryParse(value, flag) Then
Console.WriteLine("'{0}' --> {1}", value, flag)
Else
Console.WriteLine("Unable to parse '{0}'.",
If(value Is Nothing, "<null>", value))
End If
Next
End Sub
End Module
' The example displays the following output:
' Unable to parse '<null>'.
' Unable to parse ''.
' 'True' --> True
' 'False' --> False
' 'true' --> True
' 'false' --> False
' ' true ' --> True
' Unable to parse '0'.
' Unable to parse '1'.
' Unable to parse '-1'.
' Unable to parse 'string'.
Remarks
The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.
The value
parameter can be preceded or followed by white space. The comparison is ordinal and case-insensitive.
See also
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: