Guid.TryParse Guid.TryParse Guid.TryParse Guid.TryParse Method

定義

多載

TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid)
TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid)

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

TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid)

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> *  -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), ByRef result As Guid) As Boolean

參數

result
Guid Guid Guid Guid

傳回

TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid) 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 *  -> bool
Public Shared Function TryParse (input As String, ByRef result As Guid) As Boolean

參數

input
String String String String

要轉換的 GUID。The GUID to convert.

result
Guid Guid Guid Guid

要包含所剖析之值的結構。The structure that will 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.

傳回

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

範例

下列範例會建立新的 GUID,然後將它轉換成三個不同的字串表示法中,藉由呼叫ToString(String)方法以"B"、"D",而"X"格式規範,並接著呼叫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.

using System;

public class Example
{
   public static void Main()
   {
      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.
      Guid newGuid;
      foreach (var stringGuid in stringGuids) {
         if (Guid.TryParse(stringGuid, out newGuid))
            Console.WriteLine("Converted {0} to a Guid", stringGuid);
         else
            Console.WriteLine("Unable to convert {0} to a Guid", 
                              stringGuid);
      }                                      
   }
}
// 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
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,它會傳回false如果inputnull或不在可辨識的格式,而且不會擲回例外狀況。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}}
另請參閱

適用於