C# Guid ParseExact

Description

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

Syntax

Guid.ParseExact has the following syntax.


public static Guid ParseExact(
  string input,
  string format
)

Parameters

Guid.ParseExact has the following parameters.

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

Returns

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

Example

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.


//ww w .  jav  a2  s.  com
using System;

public class Example
{
   public static void Main()
   {
      string[] formats = { "N", "D", "B", "P", "X" };
      Guid guid = Guid.NewGuid();

      string[] 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) {
         Guid newGuid = Guid.ParseExact(stringGuid, "B");
         Console.WriteLine("Successfully parsed {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