Guid.TryParse Método

Definición

Sobrecargas

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

Intenta analizar un intervalo de caracteres en un valor.

TryParse(String, IFormatProvider, Guid)

Intenta analizar una cadena en un valor.

TryParse(ReadOnlySpan<Char>, Guid)

Convierte el intervalo de caracteres de solo lectura especificado que contiene la representación de un GUID en la estructura Guid equivalente.

TryParse(String, Guid)

Convierte la representación de cadena de un identificador GUID en la estructura Guid equivalente.

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

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

Intenta analizar un intervalo de caracteres en un valor.

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

Parámetros

s
ReadOnlySpan<Char>

Intervalo de caracteres que se van a analizar.

provider
IFormatProvider

Un objeto que proporciona información de formato específica de la referencia cultural sobre s.

result
Guid

Cuando este método devuelve , contiene el resultado de analizar scorrectamente o un valor indefinido en caso de error.

Devoluciones

true es si s se ha analizado correctamente; en caso contrario, falsees .

Se aplica a

TryParse(String, IFormatProvider, Guid)

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

Intenta analizar una cadena en un valor.

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

Parámetros

s
String

Cadena que se va a analizar.

provider
IFormatProvider

Un objeto que proporciona información de formato específica de la referencia cultural sobre s.

result
Guid

Cuando este método vuelve, contiene el resultado del análisis s correcto o de un valor indefinido en caso de error.

Devoluciones

true es si s se ha analizado correctamente; en caso contrario, falsees .

Se aplica a

TryParse(ReadOnlySpan<Char>, Guid)

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

Convierte el intervalo de caracteres de solo lectura especificado que contiene la representación de un GUID en la estructura Guid equivalente.

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

Parámetros

input
ReadOnlySpan<Char>

Intervalo que contiene los caracteres que representan el GUID que se va a convertir.

result
Guid

Cuando este método devuelve un resultado, contiene el valor analizado. Si el método devuelve true, result contiene un Guidválido. Si el método devuelve false, result es igual a Empty.

Devoluciones

Es true si la operación de análisis se realizó correctamente; de lo contrario, es false.

Se aplica a

TryParse(String, Guid)

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

Convierte la representación de cadena de un identificador GUID en la estructura Guid equivalente.

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

Parámetros

input
String

Cadena que contiene el GUID que se va convertir.

result
Guid

Cuando este método devuelve un resultado, contiene el valor analizado. Si el método devuelve true, result contiene un Guidválido. Si el método devuelve false, result es igual a Empty.

Devoluciones

Es true si la operación de análisis se realizó correctamente; de lo contrario, es false.

Ejemplos

En el ejemplo siguiente se crea un nuevo GUID, se convierte en tres representaciones de cadena independientes llamando al ToString(String) método con los especificadores de formato "B", "D" y "X" y, a continuación, llama al TryParse método para convertir las cadenas de nuevo en Guid valores.

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

Comentarios

Este método es similar al Parse método , excepto que, en lugar de devolver el GUID analizado, devuelve false si input está null o no en un formato reconocido y no produce una excepción. Recorta cualquier espacio en blanco inicial o final de input y convierte cadenas en cualquiera de los cinco formatos reconocidos por los ToString(String) métodos y ToString(String, IFormatProvider) , como se muestra en la tabla siguiente.

Especificador Descripción Formato
N 32 dígitos 00000000000000000000000000000000
D 32 dígitos separados por guiones 00000000-0000-0000-0000-000000000000
B 32 dígitos separados por guiones, entre llaves {00000000-0000-0000-0000-000000000000}
P 32 dígitos separados por guiones, entre paréntesis (00000000-0000-0000-0000-000000000000)
X Cuatro valores hexadecimales entre llaves, donde el cuarto valor es un subconjunto de ocho valores hexadecimales que también se incluyen entre llaves. {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

Consulte también

Se aplica a