EN
C# / .NET - create GUID value
4 points
In C# / .NET it is possible to create GUID / UUID (Globally Unique Identifier / Universally Unique Identifier) in the following way.
xxxxxxxxxx
1
Guid guid = Guid.NewGuid();
2
string text = guid.ToString();
3
4
Console.WriteLine(text);
Output:
xxxxxxxxxx
1
805036a5-254b-49f5-aef5-9b66255da3e3
xxxxxxxxxx
1
Guid guid = Guid.NewGuid();
2
byte[] bytes = guid.ToByteArray();
3
4
foreach(byte entry in bytes)
5
Console.Write(entry + " ");
6
7
Console.WriteLine();
Output:
xxxxxxxxxx
1
165 54 80 128 75 37 245 73 174 245 155 102 37 93 163 227
xxxxxxxxxx
1
Guid guid = new Guid(new byte[]
2
{
3
0x00, 0x01, 0x02, 0x03,
4
0x04, 0x05,
5
0x06, 0x07,
6
0x08, 0x09,
7
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
8
});
9
10
Console.WriteLine(guid.ToString());
Output:
xxxxxxxxxx
1
03020100-0504-0706-0809-0a0b0c0d0e0f
Note: order of first 8 bytes is different than in string. It is caused of Microsoft GUID implementation and Little-endian of x86 processors compatibility:
xxxxxxxxxx
1
typedef struct _GUID {
2
DWORD Data1;
3
WORD Data2;
4
WORD Data3;
5
BYTE Data4[8];
6
} GUID;
It means DWORD and WORD bytes are reversed.