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)

GUID の表現を含む文字の指定した読み取り専用のスパンを、等価の Guid 構造体に変換します。Converts the specified read-only span of characters containing the representation of a GUID to the equivalent Guid structure.

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)

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 GUID to convert.

result
Guid Guid Guid Guid

解析された値を格納する構造体。The structure to 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.

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 * Guid -> 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 を作成し、"B"、"D"、"X" の各書式指定子を使用して ToString(String) メソッドを呼び出して3つの個別の文字列形式に変換した後、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 を返すのではなく、inputnull であるか、認識された形式ではない場合は 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) の各メソッドによって認識される5つの形式の文字列を変換します。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つの16進値は中かっこで囲みます。4番目の値は、中かっこで囲まれた8つの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}}
こちらもご覧ください

適用対象