Guid.TryParse Méthode

Définition

Surcharges

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

Tente d’analyser une étendue de caractères dans une valeur.

TryParse(String, IFormatProvider, Guid)

Tente d’analyser une chaîne dans une valeur.

TryParse(ReadOnlySpan<Char>, Guid)

Convertit l’étendue de caractères en lecture seule spécifiée contenant la représentation d’un GUID en la structure Guid équivalente.

TryParse(String, Guid)

Convertit la représentation sous forme de chaîne d'un GUID en une structure Guid équivalente.

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Tente d’analyser une étendue de caractères dans une valeur.

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] Guid % result) = ISpanParsable<Guid>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out Guid result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * Guid -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Guid) As Boolean

Paramètres

s
ReadOnlySpan<Char>

Étendue de caractères à analyser.

provider
IFormatProvider

Objet qui fournit des informations de mise en forme propres à la culture concernant s.

result
Guid

Lorsque cette méthode est retournée, contient le résultat de l’analyse sréussie ou d’une valeur non définie en cas d’échec.

Retours

true si s a été correctement analysé ; sinon, false.

S’applique à

TryParse(String, IFormatProvider, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Tente d’analyser une chaîne dans une valeur.

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] Guid % result) = IParsable<Guid>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out Guid result);
static member TryParse : string * IFormatProvider * Guid -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Guid) As Boolean

Paramètres

s
String

Chaîne à analyser.

provider
IFormatProvider

Objet qui fournit des informations de mise en forme propres à la culture concernant s.

result
Guid

Lorsque cette méthode est retournée, contient le résultat de l’analyse s réussie ou une valeur non définie en cas d’échec.

Retours

true si s a été correctement analysé ; sinon, false.

S’applique à

TryParse(ReadOnlySpan<Char>, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Convertit l’étendue de caractères en lecture seule spécifiée contenant la représentation d’un GUID en la structure Guid équivalente.

public:
 static bool TryParse(ReadOnlySpan<char> input, [Runtime::InteropServices::Out] Guid % result);
public static bool TryParse (ReadOnlySpan<char> input, out Guid result);
static member TryParse : ReadOnlySpan<char> * Guid -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), ByRef result As Guid) As Boolean

Paramètres

input
ReadOnlySpan<Char>

Étendue contenant les caractères représentant le GUID à convertir.

result
Guid

Quand cette méthode est retournée, contient la valeur analysée. Si la méthode retourne true, result contient un Guidvalide. Si la méthode retourne false, result est égal à Empty.

Retours

true si l'opération d'analyse réussit ; sinon, false.

S’applique à

TryParse(String, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Convertit la représentation sous forme de chaîne d'un GUID en une structure Guid équivalente.

public:
 static bool TryParse(System::String ^ input, [Runtime::InteropServices::Out] Guid % result);
public static bool TryParse (string input, out Guid result);
public static bool TryParse (string? input, out Guid result);
static member TryParse : string * Guid -> bool
Public Shared Function TryParse (input As String, ByRef result As Guid) As Boolean

Paramètres

input
String

Chaîne contenant le GUID à convertir.

result
Guid

Quand cette méthode est retournée, contient la valeur analysée. Si la méthode retourne true, result contient un Guidvalide. Si la méthode retourne false, result est égal à Empty.

Retours

true si l'opération d'analyse réussit ; sinon, false.

Exemples

L’exemple suivant crée un GUID, le convertit en trois représentations de chaîne distinctes en appelant la ToString(String) méthode avec les spécificateurs de format « B », « D » et « X », puis appelle la TryParse méthode pour convertir les chaînes en Guid valeurs.

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)
{
    if (Guid.TryParse(stringGuid, out var newGuid))
        Console.WriteLine($"Converted {stringGuid} to a Guid");
    else
        Console.WriteLine($"Unable to convert {stringGuid} to a Guid");
}

// The example displays output similar to the following:
//
//    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
open System

let originalGuid = Guid.NewGuid()

// Create an array of string representations of the GUID.
let stringGuids =
    [| originalGuid.ToString "B"
       originalGuid.ToString "D"
       originalGuid.ToString "X" |]

// Parse each string representation.
for stringGuid in stringGuids do
    match Guid.TryParse stringGuid with
    | true, newGuid ->
        printfn $"Converted {stringGuid} to a Guid"
    | _ ->
        printfn $"Unable to convert {stringGuid} to a Guid"

// The example displays output similar to the following:
//
//    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
Module Example
   Public Sub Main()
      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.
      Dim newGuid As Guid
      For Each stringGuid In stringGuids
         If Guid.TryParse(stringGuid, newGuid) Then
            Console.WriteLine("Converted {0} to a Guid", stringGuid)
         Else
            Console.WriteLine("Unable to convert {0} to a Guid", 
                              stringGuid)
         End If     
      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

Remarques

Cette méthode est semblable à la Parse méthode , sauf qu’au lieu de retourner le GUID analysé, elle retourne false si input est null ou non dans un format reconnu et ne lève pas d’exception. Il supprime les espaces blancs de début ou de fin de input et convertit les chaînes dans l’un des cinq formats reconnus par les ToString(String) méthodes et ToString(String, IFormatProvider) , comme indiqué dans le tableau suivant.

Spécificateur Description Format
N 32 chiffres 00000000000000000000000000000000
D 32 chiffres séparés par des traits d’union 00000000-0000-0000-0000-000000000000
B 32 chiffres séparés par des traits d’union, entourés d’accolades {00000000-0000-0000-0000-000000000000}
P 32 chiffres séparés par des traits d’union, entre parenthèses (00000000-0000-0000-0000-000000000000)
X Quatre valeurs hexadécimales placées entre accolades, où la quatrième valeur est un sous-ensemble de huit valeurs hexadécimales qui est également placé entre accolades {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

Voir aussi

S’applique à