Guid.TryParse Metoda

Definicja

Przeciążenia

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

Próbuje przeanalizować zakres znaków w wartości.

TryParse(String, IFormatProvider, Guid)

Próbuje przeanalizować ciąg w wartości.

TryParse(ReadOnlySpan<Char>, Guid)

Konwertuje określony zakres znaków tylko do odczytu zawierający reprezentację identyfikatora GUID na równoważną Guid strukturę.

TryParse(String, Guid)

Konwertuje reprezentację ciągu identyfikatora GUID na równoważną Guid strukturę.

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

Źródło:
Guid.cs
Źródło:
Guid.cs
Źródło:
Guid.cs

Próbuje przeanalizować zakres znaków w wartości.

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

Parametry

s
ReadOnlySpan<Char>

Zakres znaków do przeanalizowania.

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.s

result
Guid

Gdy ta metoda zwraca wartość , zawiera wynik pomyślnej analizy s, lub niezdefiniowanej wartości w przypadku błędu.

Zwraca

truejeśli s została pomyślnie przeanalizowana; w przeciwnym razie . false

Dotyczy

TryParse(String, IFormatProvider, Guid)

Źródło:
Guid.cs
Źródło:
Guid.cs
Źródło:
Guid.cs

Próbuje przeanalizować ciąg w wartości.

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

Parametry

s
String

Ciąg do analizy.

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.s

result
Guid

Gdy ta metoda zwraca wartość , zawiera wynik pomyślnej analizy s lub niezdefiniowanej wartości w przypadku błędu.

Zwraca

truejeśli s została pomyślnie przeanalizowana; w przeciwnym razie . false

Dotyczy

TryParse(ReadOnlySpan<Char>, Guid)

Źródło:
Guid.cs
Źródło:
Guid.cs
Źródło:
Guid.cs

Konwertuje określony zakres znaków tylko do odczytu zawierający reprezentację identyfikatora GUID na równoważną Guid strukturę.

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

Parametry

input
ReadOnlySpan<Char>

Zakres zawierający znaki reprezentujące identyfikator GUID do konwersji.

result
Guid

Gdy ta metoda zwraca wartość , zawiera przeanalizowaną wartość. Jeśli metoda zwraca truewartość , result zawiera prawidłowy Guidelement . Jeśli metoda zwraca falsewartość , result jest równa Empty.

Zwraca

true jeśli operacja analizy zakończyła się pomyślnie; w przeciwnym razie , false.

Dotyczy

TryParse(String, Guid)

Źródło:
Guid.cs
Źródło:
Guid.cs
Źródło:
Guid.cs

Konwertuje reprezentację ciągu identyfikatora GUID na równoważną Guid strukturę.

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

Parametry

input
String

Ciąg zawierający identyfikator GUID do konwersji.

result
Guid

Gdy ta metoda zwraca wartość , zawiera przeanalizowaną wartość. Jeśli metoda zwraca truewartość , result zawiera prawidłowy Guidelement . Jeśli metoda zwraca falsewartość , result jest równa Empty.

Zwraca

true jeśli operacja analizy zakończyła się pomyślnie; w przeciwnym razie , false.

Przykłady

Poniższy przykład tworzy nowy identyfikator GUID, konwertuje go na trzy oddzielne reprezentacje ciągów, wywołując ToString(String) metodę z specyfikatorami formatu "B", "D" i "X", a następnie wywołuje TryParse metodę , aby przekonwertować ciągi z powrotem na Guid wartości.

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

Uwagi

Ta metoda jest podobna Parse do metody, z tą różnicą, że zamiast zwracać przeanalizowany identyfikator GUID, zwraca false wartość , jeśli input jest lub nie jest null w rozpoznanym formacie i nie zgłasza wyjątku. Przycina wszystkie wiodące lub końcowe białe znaki z input i konwertuje ciągi w dowolnym z pięciu formatów rozpoznawanych przez ToString(String) metody i ToString(String, IFormatProvider) , jak pokazano w poniższej tabeli.

Specyfikator Opis Format
N 32 cyfry 00000000000000000000000000000000
D 32 cyfry oddzielone łącznikami 00000000-0000-0000-0000-000000000000
B 32 cyfry oddzielone łącznikami, ujęte w nawiasy klamrowe {00000000-0000-0000-0000-000000000000}
P 32 cyfry oddzielone łącznikami, ujęte w nawiasy (00000000-0000-0000-0000-000000000000)
X Cztery wartości szesnastkowe ujęte w nawiasy klamrowe, gdzie czwarta wartość jest podzbiorem ośmiu wartości szesnastkowe, które są również ujęte w nawiasy klamrowe {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}

Zobacz też

Dotyczy