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 には有効な Guid が格納されます。If the method returns true, result contains a valid Guid. メソッドが false を返す場合、resultEmpty と同じです。If the method returns false, result equals Empty.

戻り値

解析操作が正常に実行された場合は true。それ以外の場合は falsetrue if the parse operation was successful; otherwise, false.

次の例では、新しい GUID を作成、呼び出すことによって 3 つの別個の文字列表現に変換、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によって認識される 5 つの形式のいずれかで文字列に変換して、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 4 番目の値が中かっこで囲まれた 8 つの 16 進値のサブセットをかっこで囲まれた 4 つの 16 進数の値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}}
こちらもご覧ください

適用対象