Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Guid.TryParse Method

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static bool TryParse(
	string input,
	out Guid result
)

Parameters

input
Type: System.String
The GUID to convert.
result
Type: System.Guid%
The structure that will contain the parsed value.

Return Value

Type: System.Boolean
true if the parse operation was successful; otherwise, false.

This method returns false if input is null or not in a recognized format, and does not throw an exception.

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 TryParse method to convert the strings back to Guid values.


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.
      Guid newGuid;
      foreach (var stringGuid in stringGuids) {
         if (Guid.TryParse(stringGuid, out newGuid))
            outputBlock.Text += String.Format("Converted {0} to a Guid\n", stringGuid);
         else
            outputBlock.Text += String.Format("Unable to convert {0} to a Guid\n", 
                              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


Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft