Guid Constructors

Definition

Initializes a new instance of the Guid structure.

Overloads

Guid(Byte[])

Initializes a new instance of the Guid structure by using the specified array of bytes.

Guid(ReadOnlySpan<Byte>)

Initializes a new instance of the Guid structure by using the value represented by the specified read-only span of bytes.

Guid(String)

Initializes a new instance of the Guid structure by using the value represented by the specified string.

Guid(ReadOnlySpan<Byte>, Boolean)
Guid(Int32, Int16, Int16, Byte[])

Initializes a new instance of the Guid structure by using the specified integers and byte array.

Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)

Initializes a new instance of the Guid structure by using the specified integers and bytes.

Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)

Initializes a new instance of the Guid structure by using the specified unsigned integers and bytes.

Guid(Byte[])

Initializes a new instance of the Guid structure by using the specified array of bytes.

public:
 Guid(cli::array <System::Byte> ^ b);
public Guid (byte[] b);
new Guid : byte[] -> Guid
Public Sub New (b As Byte())

Parameters

b
Byte[]

A 16-element byte array containing values with which to initialize the GUID.

Exceptions

b is not 16 bytes long.

Applies to

Guid(ReadOnlySpan<Byte>)

Initializes a new instance of the Guid structure by using the value represented by the specified read-only span of bytes.

public:
 Guid(ReadOnlySpan<System::Byte> b);
public Guid (ReadOnlySpan<byte> b);
new Guid : ReadOnlySpan<byte> -> Guid
Public Sub New (b As ReadOnlySpan(Of Byte))

Parameters

b
ReadOnlySpan<Byte>

A read-only span containing the bytes representing the GUID. The span must be exactly 16 bytes long.

Exceptions

The span must be exactly 16 bytes long.

Applies to

Guid(String)

Initializes a new instance of the Guid structure by using the value represented by the specified string.

public:
 Guid(System::String ^ g);
public Guid (string g);
new Guid : string -> Guid
Public Sub New (g As String)

Parameters

g
String

A string that contains a GUID in one of the following formats ("d" represents a hexadecimal digit whose case is ignored):

32 contiguous hexadecimal digits:

dddddddddddddddddddddddddddddddd

-or-

Groups of 8, 4, 4, 4, and 12 hexadecimal digits with hyphens between the groups. The entire GUID can optionally be enclosed in matching braces or parentheses:

dddddddd-dddd-dddd-dddd-dddddddddddd

-or-

{dddddddd-dddd-dddd-dddd-dddddddddddd}

-or-

(dddddddd-dddd-dddd-dddd-dddddddddddd)

-or-

Groups of 8, 4, and 4 hexadecimal digits, and a subset of eight groups of 2 hexadecimal digits, with each group prefixed by "0x" or "0X", and separated by commas. The entire GUID, as well as the subset, is enclosed in matching braces:

{0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}

All braces, commas, and "0x" prefixes are required. All embedded spaces are ignored. All leading zeros in a group are ignored.

The hexadecimal digits shown in a group are the maximum number of meaningful hexadecimal digits that can appear in that group. You can specify from 1 to the number of hexadecimal digits shown for a group. The specified digits are assumed to be the low-order digits of the group.

Exceptions

The format of g is invalid.

The format of g is invalid.

Examples

The following example passes each string listed in the Remarks section to the Guid(String) constructor.

string[] guidStrings = { "ca761232ed4211cebacd00aa0057b223",
                         "CA761232-ED42-11CE-BACD-00AA0057B223",
                         "{CA761232-ED42-11CE-BACD-00AA0057B223}",
                         "(CA761232-ED42-11CE-BACD-00AA0057B223)",
                         "{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}" };
foreach (var guidString in guidStrings)
{
    var guid = new Guid(guidString);
    Console.WriteLine($"Original string: {guidString}");
    Console.WriteLine($"Guid:            {guid}");
    Console.WriteLine();
}

// The example displays the following output:
//    Original string: ca761232ed4211cebacd00aa0057b223
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
//
//    Original string: CA761232-ED42-11CE-BACD-00AA0057B223
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
//
//    Original string: {CA761232-ED42-11CE-BACD-00AA0057B223}
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
//
//    Original string: (CA761232-ED42-11CE-BACD-00AA0057B223)
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
//
//    Original string: {0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
open System

let guidStrings =
    [ "ca761232ed4211cebacd00aa0057b223"
      "CA761232-ED42-11CE-BACD-00AA0057B223"
      "{CA761232-ED42-11CE-BACD-00AA0057B223}"
      "(CA761232-ED42-11CE-BACD-00AA0057B223)"
      "{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}" ]

for guidString in guidStrings do
    let guid = Guid guidString
    printfn $"Original string: {guidString}"
    printfn $"Guid:            {guid}\n"

// The example displays the following output:
//    Original string: ca761232ed4211cebacd00aa0057b223
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
//
//    Original string: CA761232-ED42-11CE-BACD-00AA0057B223
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
//
//    Original string: {CA761232-ED42-11CE-BACD-00AA0057B223}
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
//
//    Original string: (CA761232-ED42-11CE-BACD-00AA0057B223)
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
//
//    Original string: {0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}
//    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
Module Example
   Public Sub Main()
      Dim guidStrings() As String = { "ca761232ed4211cebacd00aa0057b223",
                                      "CA761232-ED42-11CE-BACD-00AA0057B223", 
                                      "{CA761232-ED42-11CE-BACD-00AA0057B223}", 
                                      "(CA761232-ED42-11CE-BACD-00AA0057B223)", 
                                      "{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}" }
      For Each guidString In guidStrings
         Dim guid As New Guid(guidString)
         Console.WriteLine("Original string: {0}", guidString)
         Console.WriteLine("Guid:            {0}", guid)
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'    Original string: ca761232ed4211cebacd00aa0057b223
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
'    
'    Original string: CA761232-ED42-11CE-BACD-00AA0057B223
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
'    
'    Original string: {CA761232-ED42-11CE-BACD-00AA0057B223}
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
'    
'    Original string: (CA761232-ED42-11CE-BACD-00AA0057B223)
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223
'    
'    Original string: {0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}
'    Guid:            ca761232-ed42-11ce-bacd-00aa0057b223

Remarks

The alphabetic hexadecimal digits in the g parameter can be uppercase or lowercase. For example, the following strings represent the same GUID:

"ca761232ed4211cebacd00aa0057b223"

"CA761232-ED42-11CE-BACD-00AA0057B223"

"{CA761232-ED42-11CE-BACD-00AA0057B223}"

"(CA761232-ED42-11CE-BACD-00AA0057B223)"

"{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}"

Applies to

Guid(ReadOnlySpan<Byte>, Boolean)

public:
 Guid(ReadOnlySpan<System::Byte> b, bool bigEndian);
public Guid (ReadOnlySpan<byte> b, bool bigEndian);
new Guid : ReadOnlySpan<byte> * bool -> Guid
Public Sub New (b As ReadOnlySpan(Of Byte), bigEndian As Boolean)

Parameters

bigEndian
Boolean

Applies to

Guid(Int32, Int16, Int16, Byte[])

Initializes a new instance of the Guid structure by using the specified integers and byte array.

public:
 Guid(int a, short b, short c, cli::array <System::Byte> ^ d);
public Guid (int a, short b, short c, byte[] d);
new Guid : int * int16 * int16 * byte[] -> Guid
Public Sub New (a As Integer, b As Short, c As Short, d As Byte())

Parameters

a
Int32

The first 4 bytes of the GUID.

b
Int16

The next 2 bytes of the GUID.

c
Int16

The next 2 bytes of the GUID.

d
Byte[]

The remaining 8 bytes of the GUID.

Exceptions

d is not 8 bytes long.

Examples

Guid(1,2,3,new byte[]{0,1,2,3,4,5,6,7}) creates a Guid that corresponds to "00000001-0002-0003-0001-020304050607".

Applies to

Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)

Initializes a new instance of the Guid structure by using the specified integers and bytes.

public:
 Guid(int a, short b, short c, System::Byte d, System::Byte e, System::Byte f, System::Byte g, System::Byte h, System::Byte i, System::Byte j, System::Byte k);
public Guid (int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k);
new Guid : int * int16 * int16 * byte * byte * byte * byte * byte * byte * byte * byte -> Guid
Public Sub New (a As Integer, b As Short, c As Short, d As Byte, e As Byte, f As Byte, g As Byte, h As Byte, i As Byte, j As Byte, k As Byte)

Parameters

a
Int32

The first 4 bytes of the GUID.

b
Int16

The next 2 bytes of the GUID.

c
Int16

The next 2 bytes of the GUID.

d
Byte

The next byte of the GUID.

e
Byte

The next byte of the GUID.

f
Byte

The next byte of the GUID.

g
Byte

The next byte of the GUID.

h
Byte

The next byte of the GUID.

i
Byte

The next byte of the GUID.

j
Byte

The next byte of the GUID.

k
Byte

The next byte of the GUID.

Examples

The following example creates a GUID whose string representation is "0000000a-000b-000c-0001-020304050607".

var g = new Guid(0xA, 0xB, 0xC,
                  new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7 } );
Console.WriteLine($"{g:B}");

// The example displays the following output:
//        {0000000a-000b-000c-0001-020304050607}
open System

let g = Guid(0xA, 0xBs, 0xCs, [| 0uy..7uy |])
printfn $"{g:B}"

// The example displays the following output:
//        {0000000a-000b-000c-0001-020304050607}
Module Example
   Public Sub Main()
      Dim g As New Guid(&hA, &hB, &hC, 
                        New Byte() { 0, 1, 2, 3, 4, 5, 6, 7 } )
      Console.WriteLine("{0:B}", g)
   End Sub
End Module
' The example displays the following output:
'   {0000000a-000b-000c-0001-020304050607}

Remarks

Specifying individual bytes in this manner can be used to circumvent byte order restrictions (big-endian or little-endian byte order) on particular types of computers.

Applies to

Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)

Important

This API is not CLS-compliant.

Initializes a new instance of the Guid structure by using the specified unsigned integers and bytes.

public:
 Guid(System::UInt32 a, System::UInt16 b, System::UInt16 c, System::Byte d, System::Byte e, System::Byte f, System::Byte g, System::Byte h, System::Byte i, System::Byte j, System::Byte k);
[System.CLSCompliant(false)]
public Guid (uint a, ushort b, ushort c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k);
[<System.CLSCompliant(false)>]
new Guid : uint32 * uint16 * uint16 * byte * byte * byte * byte * byte * byte * byte * byte -> Guid
Public Sub New (a As UInteger, b As UShort, c As UShort, d As Byte, e As Byte, f As Byte, g As Byte, h As Byte, i As Byte, j As Byte, k As Byte)

Parameters

a
UInt32

The first 4 bytes of the GUID.

b
UInt16

The next 2 bytes of the GUID.

c
UInt16

The next 2 bytes of the GUID.

d
Byte

The next byte of the GUID.

e
Byte

The next byte of the GUID.

f
Byte

The next byte of the GUID.

g
Byte

The next byte of the GUID.

h
Byte

The next byte of the GUID.

i
Byte

The next byte of the GUID.

j
Byte

The next byte of the GUID.

k
Byte

The next byte of the GUID.

Attributes

Remarks

Specifying the bytes in this manner avoids endianness issues.

Applies to