Guid Constructors

Definition

初始化 Guid 结构的新实例。Initializes a new instance of the Guid structure.

Overloads

Guid(Byte[])

使用指定的字节数组初始化 Guid 类的新实例。Initializes a new instance of the Guid structure by using the specified array of bytes.

Guid(ReadOnlySpan<Byte>)

通过使用指定的只读字节范围所表示的值来初始化 Guid 结构的新实例。Initializes a new instance of the Guid structure by using the value represented by the specified read-only span of bytes.

Guid(String)

使用指定字符串所表示的值初始化 Guid 类的新实例。Initializes a new instance of the Guid structure by using the value represented by the specified string.

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

使用指定的整数和字节数组初始化 Guid 类的新实例。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)

使用指定的整数和字节初始化 Guid 类的新实例。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)

使用指定的无符号整数和字节初始化 Guid 类的新实例。Initializes a new instance of the Guid structure by using the specified unsigned integers and bytes.

Guid(Byte[])

使用指定的字节数组初始化 Guid 类的新实例。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[]

包含用于初始化 GUID 的值的 16 元素字节数组。A 16-element byte array containing values with which to initialize the GUID.

Exceptions

bnullb is null.

b 的长度不是 16 个字节。b is not 16 bytes long.

Guid(ReadOnlySpan<Byte>)

通过使用指定的只读字节范围所表示的值来初始化 Guid 结构的新实例。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>

包含表示 GUID 的字节的只读范围。A read-only span containing the bytes representing the GUID. 范围的长度必须正好为 16 个字节。The span must be exactly 16 bytes long.

Exceptions

范围的长度必须正好为 16 个字节。The span must be exactly 16 bytes long.

Guid(String)

使用指定字符串所表示的值初始化 Guid 类的新实例。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

包含下面任一格式的 GUID 的字符串(“d”表示忽略大小写的十六进制数字):A string that contains a GUID in one of the following formats ("d" represents a hexadecimal digit whose case is ignored):

32 个连续的数字:32 contiguous digits:

dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

- 或 --or-

8、4、4、4 和 12 位数字的分组,各组之间有连线符。Groups of 8, 4, 4, 4, and 12 digits with hyphens between the groups. 也可以用一对大括号或者圆括号将整个 GUID 括起来:The entire GUID can optionally be enclosed in matching braces or parentheses:

dddddddd-dddd-dddd-dddd-dddddddddddddddddddd-dddd-dddd-dddd-dddddddddddd

- 或 --or-

{dddddddd-dddd-dddd-dddd-dddddddddddd}{dddddddd-dddd-dddd-dddd-dddddddddddd}

- 或 --or-

(dddddddd-dddd-dddd-dddd-dddddddddddd)(dddddddd-dddd-dddd-dddd-dddddddddddd)

- 或 --or-

8、4 和 4 位数字的分组,和一个 8 组 2 位数字的子集,每组都带有前缀“0x”或“0X”,以逗号分隔。Groups of 8, 4, and 4 digits, and a subset of eight groups of 2 digits, with each group prefixed by "0x" or "0X", and separated by commas. 整个 GUID 和子集用大括号括起来:The entire GUID, as well as the subset, is enclosed in matching braces:

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

所有大括号、逗号和“0x”前缀都是必需的。All braces, commas, and "0x" prefixes are required. 所有内置的空格都将被忽略。All embedded spaces are ignored. 组中的所有前导零都将被忽略。All leading zeros in a group are ignored.

组中显示的数字为可在该组显示的有意义数字的最大数目。The digits shown in a group are the maximum number of meaningful digits that can appear in that group. 你可以指定从 1 到为组显示的位数。You can specify from 1 to the number of digits shown for a group. 指定的位数被认为是该组低序位的位数。The specified digits are assumed to be the low-order digits of the group.

Exceptions

gnullg is null.

g 的格式无效。The format of g is invalid.

g 的格式无效。The format of g is invalid.

Examples

下面的示例将 "备注" 部分中列出的每个字符串传递到 Guid(String) 构造函数。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
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

g 参数中的字母十六进制数字可以是大写或小写。The alphabetic hexadecimal digits in the g parameter can be uppercase or lowercase. 例如,以下字符串表示相同的 GUID:For example, the following strings represent the same GUID:

"ca761232ed4211cebacd00aa0057b223""ca761232ed4211cebacd00aa0057b223"

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

"{CA761232-ED42-11CE-BACD-00AA0057B223}""{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}}""{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}"

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

使用指定的整数和字节数组初始化 Guid 类的新实例。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

GUID 的前 4 个字节。The first 4 bytes of the GUID.

b
Int16

GUID 的下两个字节。The next 2 bytes of the GUID.

c
Int16

GUID 的下两个字节。The next 2 bytes of the GUID.

d
Byte[]

GUID 的其余 8 个字节。The remaining 8 bytes of the GUID.

Exceptions

dnulld is null.

d 的长度不是 8 个字节。d is not 8 bytes long.

Examples

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

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

使用指定的整数和字节初始化 Guid 类的新实例。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

GUID 的前 4 个字节。The first 4 bytes of the GUID.

b
Int16

GUID 的下两个字节。The next 2 bytes of the GUID.

c
Int16

GUID 的下两个字节。The next 2 bytes of the GUID.

d
Byte

GUID 的下一个字节。The next byte of the GUID.

e
Byte

GUID 的下一个字节。The next byte of the GUID.

f
Byte

GUID 的下一个字节。The next byte of the GUID.

g
Byte

GUID 的下一个字节。The next byte of the GUID.

h
Byte

GUID 的下一个字节。The next byte of the GUID.

i
Byte

GUID 的下一个字节。The next byte of the GUID.

j
Byte

GUID 的下一个字节。The next byte of the GUID.

k
Byte

GUID 的下一个字节。The next byte of the GUID.

Examples

下面的示例创建一个 GUID,其字符串表示形式为 "0000000a-000b-000c-0001-020304050607"。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}
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.

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

Important

This API is not CLS-compliant.

使用指定的无符号整数和字节初始化 Guid 类的新实例。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);
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

GUID 的前 4 个字节。The first 4 bytes of the GUID.

b
UInt16

GUID 的下两个字节。The next 2 bytes of the GUID.

c
UInt16

GUID 的下两个字节。The next 2 bytes of the GUID.

d
Byte

GUID 的下一个字节。The next byte of the GUID.

e
Byte

GUID 的下一个字节。The next byte of the GUID.

f
Byte

GUID 的下一个字节。The next byte of the GUID.

g
Byte

GUID 的下一个字节。The next byte of the GUID.

h
Byte

GUID 的下一个字节。The next byte of the GUID.

i
Byte

GUID 的下一个字节。The next byte of the GUID.

j
Byte

GUID 的下一个字节。The next byte of the GUID.

k
Byte

GUID 的下一个字节。The next byte of the GUID.

Attributes

Examples

Guid (0xa,0xb,0xc,0,1,2,3,4,5,6,7)创建与 "0000000a-000b-000c-0001-020304050607" 对应的 GuidGuid(0xa,0xb,0xc,0,1,2,3,4,5,6,7) creates a Guid that corresponds to "0000000a-000b-000c-0001-020304050607".

Remarks

以这种方式指定字节可避免出现 endian 问题。Specifying the bytes in this manner avoids endianness issues.

Applies to