C# Guid Parse

Description

Guid Parse converts the string representation of a GUID to the equivalent Guid structure.

Syntax

Guid.Parse has the following syntax.


public static Guid Parse(
  string input
)

Parameters

Guid.Parse has the following parameters.

  • input - The GUID to convert.

Returns

Guid.Parse method returns A structure that contains the value that was parsed.

Example

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


/*from  w  w  w  . j  av  a2s  .c  o  m*/

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("D"),
                               originalGuid.ToString("X") };

      foreach (var stringGuid in stringGuids) {
         try {     
            Guid newGuid = Guid.Parse(stringGuid);
            Console.WriteLine("Converted {0} to a Guid", stringGuid);
         }   
         catch (ArgumentNullException) {
            Console.WriteLine("The string to be parsed is null.");   
         }                                   
         catch (FormatException) {
            Console.WriteLine("Bad format: {0}", stringGuid);
         }
      }                                      
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version