Guid.Parse Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Converts the string representation of a GUID to the equivalent Guid value.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Shared Function Parse ( _
input As String _
) As Guid
public static Guid Parse(
string input
)
Parameters
- input
Type: System.String
The GUID to convert.
Return Value
Type: System.Guid
A structure that contains the value that was parsed.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | input is nulla null reference (Nothing in Visual Basic). |
FormatException | input is not in a recognized format. |
Remarks
Use the TryParse method to catch any unsuccessful parse operations without having to explicitly handle exceptions.
Examples
The following example creates a new GUID, converts it to three separate string representations by calling the ToString method with the "B", "D", and "X" format specifiers, and then calls the Parse method to convert the strings back to Guid values.
Module Example
Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
Dim originalGuid As Guid = Guid.NewGuid()
' Create an array of string representations of the GUID.
Dim stringGuids() As String = { originalGuid.ToString("B"),
originalGuid.ToString("D"),
originalGuid.ToString("X") }
' Parse each string representation.
For Each stringGuid In stringGuids
Try
Dim newGuid As Guid = Guid.Parse(stringGuid)
outputBlock.Text += String.Format("Converted {0} to a Guid", stringGuid) + vbCrLf
Catch e As ArgumentNullException
outputBlock.Text += "The string to be parsed is null." + vbCrLf
Catch e As FormatException
outputBlock.Text += String.Format("Bad format: {0}", stringGuid) + vbCrLf
End Try
Next
End Sub
End Module
' The example displays the following output:
' 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
using System;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
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) {
try {
Guid newGuid = Guid.Parse(stringGuid);
outputBlock.Text += String.Format("Converted {0} to a Guid\n", stringGuid);
}
catch (ArgumentNullException) {
outputBlock.Text += "The string to be parsed is null.\n";
}
catch (FormatException) {
outputBlock.Text += String.Format("Bad format: {0}", stringGuid);
}
}
}
}
// The example displays the following output:
// 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
Version Information
Silverlight
Supported in: 5, 4
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.