Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
TryParse Method

Guid.TryParse Method

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

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

public static bool TryParse(
	string input,
	out Guid result


Type: System.String

The GUID to convert.

Type: System.Guid

The structure that will contain the parsed value. If the method returns true, result contains a valid Guid. If the method returns false, result equals Guid.Empty.

Return Value

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

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.





32 digits



32 digits separated by hyphens



32 digits separated by hyphens, enclosed in braces



32 digits separated by hyphens, enclosed in parentheses



Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces


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.

using System;

public class Example
   public static void Main()
      Guid originalGuid = Guid.NewGuid();
      // Create an array of string representations of the GUID. 
      string[] stringGuids = { originalGuid.ToString("B"),
                               originalGuid.ToString("X") };

      // Parse each string representation.
      Guid newGuid;
      foreach (var stringGuid in stringGuids) {
         if (Guid.TryParse(stringGuid, out newGuid))
            Console.WriteLine("Converted {0} to a Guid", stringGuid);
            Console.WriteLine("Unable to convert {0} to a Guid", 
// 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

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
© 2015 Microsoft