Прочетете на английски Редактиране

Споделяне чрез


Guid.TryParse Method

Definition

Overloads

TryParse(ReadOnlySpan<Char>, Guid)

Converts the specified read-only span of characters containing the representation of a GUID to the equivalent Guid structure.

TryParse(String, Guid)

Converts the string representation of a GUID to the equivalent Guid structure.

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

Tries to parse a span of characters into a value.

TryParse(String, IFormatProvider, Guid)

Tries to parse a string into a value.

TryParse(ReadOnlySpan<Char>, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Converts the specified read-only span of characters containing the representation of a GUID to the equivalent Guid structure.

C#
public static bool TryParse(ReadOnlySpan<char> input, out Guid result);

Parameters

input
ReadOnlySpan<Char>

A span containing the characters representing the GUID to convert.

result
Guid

When this method returns, contains the parsed value. If the method returns true, result contains a valid Guid. If the method returns false, result equals Empty.

Returns

true if the parse operation was successful; otherwise, false.

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

TryParse(String, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Converts the string representation of a GUID to the equivalent Guid structure.

C#
public static bool TryParse(string input, out Guid result);
C#
public static bool TryParse(string? input, out Guid result);

Parameters

input
String

A string containing the GUID to convert.

result
Guid

When this method returns, contains the parsed value. If the method returns true, result contains a valid Guid. If the method returns false, result equals Empty.

Returns

true if the parse operation was successful; otherwise, false.

Examples

The following example creates a new GUID, converts it to three separate string representations by calling the ToString(String) method with the "B", "D", and "X" format specifiers, and then calls the TryParse method to convert the strings back to Guid values.

C#
Guid originalGuid = Guid.NewGuid();
// Create an array of string representations of the GUID.
string[] stringGuids = { originalGuid.ToString("B"),
                         originalGuid.ToString("D"),
                         originalGuid.ToString("X") };

// Parse each string representation.
foreach (var stringGuid in stringGuids)
{
    if (Guid.TryParse(stringGuid, out var newGuid))
        Console.WriteLine($"Converted {stringGuid} to a Guid");
    else
        Console.WriteLine($"Unable to convert {stringGuid} to a Guid");
}

// The example displays output similar to the following:
//
//    Converted {81a130d2-502f-4cf1-a376-63edeb000e9f} to a Guid
//    Converted 81a130d2-502f-4cf1-a376-63edeb000e9f to a Guid
//    Converted {0x81a130d2,0x502f,0x4cf1,{0xa3,0x76,0x63,0xed,0xeb,0x00,0x0e,0x9f}} to a Guid

Remarks

This method is like the Parse method, except that instead of returning the parsed GUID, it returns false if input is null or not in a recognized format, and doesn't throw an exception. It trims any leading or trailing white space from input and converts strings in any of the five formats recognized by the ToString(String) and ToString(String, IFormatProvider) methods, as shown in the following table.

Specifier Description Format
N 32 digits 00000000000000000000000000000000
D 32 digits separated by hyphens 00000000-0000-0000-0000-000000000000
B 32 digits separated by hyphens, enclosed in braces {00000000-0000-0000-0000-000000000000}
P 32 digits separated by hyphens, enclosed in parentheses (00000000-0000-0000-0000-000000000000)
X Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

See also

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.NET Framework 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

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Tries to parse a span of characters into a value.

C#
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out Guid result);

Parameters

s
ReadOnlySpan<Char>

The span of characters to parse.

provider
IFormatProvider

An object that provides culture-specific formatting information about s.

result
Guid

When this method returns, contains the result of successfully parsing s, or an undefined value on failure.

Returns

true if s was successfully parsed; otherwise, false.

Applies to

.NET 10 и други версии
Продукт Версии
.NET 7, 8, 9, 10

TryParse(String, IFormatProvider, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Tries to parse a string into a value.

C#
public static bool TryParse(string? s, IFormatProvider? provider, out Guid result);

Parameters

s
String

The string to parse.

provider
IFormatProvider

An object that provides culture-specific formatting information about s.

result
Guid

When this method returns, contains the result of successfully parsing s or an undefined value on failure.

Returns

true if s was successfully parsed; otherwise, false.

Applies to

.NET 10 и други версии
Продукт Версии
.NET 7, 8, 9, 10