Guid.TryParse 方法

定義

多載

TryParse(ReadOnlySpan<Char>, Guid)

將包含 GUID 表示法的指定唯讀字元範圍轉換為對等的 Guid 結構。Converts the specified read-only span of characters containing the representation of a GUID to the equivalent Guid structure.

TryParse(String, Guid)

將 GUID 的字串表示轉換為對等的 Guid 結構。Converts the string representation of a GUID to the equivalent Guid structure.

TryParse(ReadOnlySpan<Char>, Guid)

將包含 GUID 表示法的指定唯讀字元範圍轉換為對等的 Guid 結構。Converts the specified read-only span of characters containing the representation of a GUID to the equivalent Guid structure.

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

參數

input
ReadOnlySpan<Char>

範圍,其包含代表所要轉換 GUID 的字元。A span containing the characters representing the GUID to convert.

result
Guid

要包含已剖析值的 Guid 執行個體。A Guid instance to contain the parsed value. 如果方法傳回 true,則 result 包含有效的 GuidIf the method returns true, result contains a valid Guid. 如果方法會傳回 false,則 result 等於 EmptyIf the method returns false, result equals Empty.

傳回

Boolean

如果剖析作業成功則為 true,否則為 falsetrue if the parse operation was successful; otherwise, false.

TryParse(String, Guid)

將 GUID 的字串表示轉換為對等的 Guid 結構。Converts the string representation of a GUID to the equivalent Guid structure.

public:
 static bool TryParse(System::String ^ input, [Runtime::InteropServices::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

參數

input
String

包含所要轉換 GUID 的字串。A string containing the GUID to convert.

result
Guid

要包含已剖析值的 Guid 執行個體。A Guid instance to contain the parsed value. 如果方法傳回 true,則 result 包含有效的 GuidIf the method returns true, result contains a valid Guid. 如果方法會傳回 false,則 result 等於 EmptyIf the method returns false, result equals Empty.

傳回

Boolean

如果剖析作業成功則為 true,否則為 falsetrue if the parse operation was successful; otherwise, false.

範例

下列範例會建立新的 GUID,將它轉換成三個不同的字串表示,方法是呼叫具有 "B"、"D" 和 "X" 格式規範的 ToString(String) 方法,然後呼叫 TryParse 方法,將字串轉換回 Guid 值。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 TryParse method to convert the strings back to Guid values.

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
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

備註

這個方法就像 Parse 方法,不同的是,它不會傳回剖析的 GUID,它會在 input null 或不是可辨識的格式時傳回 false,而且不會擲回例外狀況。This method is like the Parse method, except that instead of returning the parsed GUID, it returns false if input is null or not in a recognized format, and doesn't throw an exception. 它會修剪 input 中的任何前置或尾端空白字元,並以 ToString(String)ToString(String, IFormatProvider) 方法所識別的五種格式之一來轉換字串,如下表所示。It trims any leading or trailing white space from input and converts strings in any of the five formats recognized by the ToString(String) and ToString(String, IFormatProvider) methods, as shown in the following table.

指定名稱Specifier 描述Description [格式]Format
N 32位數32 digits 0000000000000000000000000000000000000000000000000000000000000000
D 以連字號分隔的32位數32 digits separated by hyphens 00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000
B 以連字號分隔的32位數,以大括弧括住32 digits separated by hyphens, enclosed in braces {00000000-0000-0000-0000-000000000000}
P 以連字號分隔的32位數,以括弧括住32 digits separated by hyphens, enclosed in parentheses (00000000-0000-0000-0000-000000000000)(00000000-0000-0000-0000-000000000000)
X 四個以大括弧括住的十六進位值,其中第四個值是八個十六進位值的子集,也以大括弧括住Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces {0x00000000、0x0000、0x0000、{0x00、0x00、0x00、0x00、0x00、0x00、0x00、0x00}}{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

另請參閱

適用於