Edit

Share via


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[])

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

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

C#
public Guid(byte[] b);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Guid(ReadOnlySpan<Byte>)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

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

C#
public Guid(ReadOnlySpan<byte> b);

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

.NET 10 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

Guid(String)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

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

C#
public Guid(string g);

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.

C#
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

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Guid(ReadOnlySpan<Byte>, Boolean)

Source:
Guid.cs
Source:
Guid.cs
C#
public Guid(ReadOnlySpan<byte> b, bool bigEndian);

Parameters

bigEndian
Boolean

Applies to

.NET 10 and other versions
Product Versions
.NET 8, 9, 10

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

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

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

C#
public Guid(int a, short b, short c, byte[] d);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

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

C#
public Guid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k);

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".

C#
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}

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Important

This API is not CLS-compliant.

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

C#
[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);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0