Guid.ParseExact Method

Definition

Overloads

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Converts the character span representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

ParseExact(String, String)

Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

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

Converts the character span representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

public static Guid ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format);

Parameters

input
ReadOnlySpan<Char>

A read-only span containing the characters representing the GUID to convert.

format
ReadOnlySpan<Char>

A read-only span of characters representing one of the following specifiers that indicates the exact format to use when interpreting input: "N", "D", "B", "P", or "X".

Returns

A structure that contains the value that was parsed.

Remarks

The ParseExact method requires the read-only character span to convert to be exactly in the format specified by the format parameter, after leading and trailing white-space characters are removed. The following table shows the accepted format specifiers for the format parameter. "0" represents a digit; hyphens ("-"), braces ("{", "}"), and parentheses ("(", ")") appear as shown.

Specifier Format of the input parameter
N 32 hexadecimal digits:

00000000000000000000000000000000
D 32 hexadecimal digits separated by hyphens:

00000000-0000-0000-0000-000000000000
B 32 hexadecimal digits separated by hyphens, enclosed in braces:

{00000000-0000-0000-0000-000000000000}
P 32 hexadecimal 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}}

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

ParseExact(String, String)

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

Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

public static Guid ParseExact (string input, string format);

Parameters

input
String

The GUID to convert.

format
String

One of the following specifiers that indicates the exact format to use when interpreting input: "N", "D", "B", "P", or "X".

Returns

A structure that contains the value that was parsed.

Exceptions

input or format is null.

input is not in the format specified by format.

Examples

The following example calls the ToString(String) method with each of the supported format specifiers to generate an array of strings that represent a single GUID. These are then passed to the ParseExact method, which successfully parses only the string that conforms to the "B" format specifier.

// Define an array of all format specifiers.
string[] formats = { "N", "D", "B", "P", "X" };
Guid guid = Guid.NewGuid();
// Create an array of valid Guid string representations.
var stringGuids = new string[formats.Length];
for (int ctr = 0; ctr < formats.Length; ctr++)
    stringGuids[ctr] = guid.ToString(formats[ctr]);

// Parse the strings in the array using the "B" format specifier.
foreach (var stringGuid in stringGuids)
{
    try
    {
        Guid newGuid = Guid.ParseExact(stringGuid, "B");
        Console.WriteLine($"Successfully parsed {stringGuid}");
    }
    catch (ArgumentNullException)
    {
        Console.WriteLine("The string to be parsed is null.");
    }
    catch (FormatException)
    {
        Console.WriteLine($"Bad Format: {stringGuid}");
    }
}

// The example displays output similar to the following:
//
//    Bad Format: eb5c8c7d187a44e68afb81e854c39457
//    Bad Format: eb5c8c7d-187a-44e6-8afb-81e854c39457
//    Successfully parsed {eb5c8c7d-187a-44e6-8afb-81e854c39457}
//    Bad Format: (eb5c8c7d-187a-44e6-8afb-81e854c39457)
//    Bad Format: {0xeb5c8c7d,0x187a,0x44e6,{0x8a,0xfb,0x81,0xe8,0x54,0xc3,0x94,0x57}}

Remarks

The ParseExact method requires the string to convert to be exactly in the format specified by the format parameter, after leading and trailing white-space characters are removed. The following table shows the accepted format specifiers for the format parameter. "0" represents a digit; hyphens ("-"), braces ("{", "}"), and parentheses ("(", ")") appear as shown.

Specifier Format of the input parameter
N 32 hexadecimal digits:

00000000000000000000000000000000
D 32 hexadecimal digits separated by hyphens:

00000000-0000-0000-0000-000000000000
B 32 hexadecimal digits separated by hyphens, enclosed in braces:

{00000000-0000-0000-0000-000000000000}
P 32 hexadecimal 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 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 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