Guid.TryParse 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| TryParse(ReadOnlySpan<Char>, Guid) | 将包含 GUID 表示形式的字符的指定只读范围转换为等效 Guid 结构。 | 
| TryParse(String, Guid) | 将 GUID 的字符串表示形式转换为等效 Guid 结构。 | 
| TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid) | 尝试将字符范围分析为值。 | 
| TryParse(String, IFormatProvider, Guid) | 尝试将字符串分析为值。 | 
TryParse(ReadOnlySpan<Char>, Guid)
- Source:
- Guid.cs
- Source:
- Guid.cs
- Source:
- Guid.cs
- Source:
- Guid.cs
将包含 GUID 表示形式的字符的指定只读范围转换为等效 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> * Guid -> boolPublic Shared Function TryParse (input As ReadOnlySpan(Of Char), ByRef result As Guid) As Boolean参数
- input
- ReadOnlySpan<Char>
一个范围,包含表示要转换的 GUID 的字符。
返回
如果分析操作成功,true;否则,false。
适用于
TryParse(String, Guid)
- Source:
- Guid.cs
- Source:
- Guid.cs
- Source:
- Guid.cs
- Source:
- Guid.cs
将 GUID 的字符串表示形式转换为等效 Guid 结构。
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 -> boolPublic Shared Function TryParse (input As String, ByRef result As Guid) As Boolean参数
- input
- String
包含要转换的 GUID 的字符串。
返回
如果分析操作成功,true;否则,false。
示例
以下示例创建一个新的 GUID,通过使用“B”、“D”和“X”格式说明符调用 ToString(String) 方法,并将其转换为三个单独的字符串表示形式,然后调用 TryParse 方法将字符串转换回 Guid 值。
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
注解
此方法与 Parse 方法类似,不同之处在于,如果 inputnull 或未采用识别格式,并且不会引发异常,则此方法将返回 false。 它从 input 剪裁任何前导空格或尾随空格,并转换由 ToString(String) 和 ToString(String, IFormatProvider) 方法识别的五种格式中的任何字符串,如下表所示。
| 规范 | 描述 | 格式 | 
|---|---|---|
| N | 32 位数字 | 00000000000000000000000000000000 | 
| D | 用连字符分隔的 32 位数字 | 00000000-0000-0000-0000-000000000000 | 
| B | 用连字符分隔的 32 位数字,用大括号括起来 | {00000000-0000-0000-0000-000000000000} | 
| P | 用连字符分隔的 32 位数字,括在括号中 | (00000000-0000-0000-0000-000000000000) | 
| X | 四个十六进制值括在大括号中,其中第四个值是八个十六进制值的子集,也用大括号括起来 | {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}} | 
另请参阅
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)
- Source:
- Guid.cs
- Source:
- Guid.cs
- Source:
- Guid.cs
- Source:
- Guid.cs
尝试将字符范围分析为值。
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 -> boolPublic Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Guid) As Boolean参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s的区域性特定格式设置信息。
- result
- Guid
此方法返回时,包含成功分析 s的结果或失败时未定义的值。
返回
如果已成功分析 s,则 true;否则,false。
适用于
TryParse(String, IFormatProvider, Guid)
- Source:
- Guid.cs
- Source:
- Guid.cs
- Source:
- Guid.cs
- Source:
- Guid.cs
尝试将字符串分析为值。
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 -> boolPublic Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Guid) As Boolean参数
- s
- String
要分析的字符串。
- provider
- IFormatProvider
一个对象,提供有关 s的区域性特定格式设置信息。
- result
- Guid
此方法返回时,包含成功分析 s 或失败时未定义的值的结果。
返回
如果已成功分析 s,则 true;否则,false。