Assigning a GUID in C#
If you already have a string representation of the Guid
, you can do this:
Guid g = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00");
And if you want a brand new Guid
then just do
Guid g = Guid.NewGuid();
C# how to create a Guid value?
Guid id = Guid.NewGuid();
Generating a desired GUID in C#
This is not directly possible.
You can generate a GUID then convert to a string and replace the first section with the wanted format.
This does mean that you are reducing the entropy of the GUID.
Testing using a guid... how do I set the variable to a Guid?
Guid x = new Guid("5fb7097c-335c-4d07-b4fd-000004e2d28c");
Increment Guid in C#
You can get the byte components of the guid, so you can just work on that:
static class GuidExtensions
{
private static readonly int[] _guidByteOrder =
new[] { 15, 14, 13, 12, 11, 10, 9, 8, 6, 7, 4, 5, 0, 1, 2, 3 };
public static Guid Increment(this Guid guid)
{
var bytes = guid.ToByteArray();
bool carry = true;
for (int i = 0; i < _guidByteOrder.Length && carry; i++)
{
int index = _guidByteOrder[i];
byte oldValue = bytes[index]++;
carry = oldValue > bytes[index];
}
return new Guid(bytes);
}
}
EDIT: now with correct byte order
Generate a Guid on compile time
(Much of this answer "promoted" from a comment to the question.)
In Visual Studio, you can choose "Tools" - "Create GUID". Or in Windows PowerShell you can say [Guid]::NewGuid().ToString()
. That will give you a Guid
. Then you can make the string representation of that particular Guid your string
constant.
public const string ExtensionKey = "2f07b447-f1ba-418b-8065-5571567e63f6";
The Guid
is fixed, of course. A field marked const
is always static
(but you must not supply the static
keyword; it is implied).
If you want to have the field of Guid
type, then it can't be declared const
in C#. Then you would do:
public static readonly Guid ExtensionKey = new Guid("2f07b447-f1ba-418b-8065-5571567e63f6");
readonly
means that the field can only be changed from a (static
in this case) constructor of the same class (a constructor of a derived class is not OK).
Create a consistant GUID or unique identifier from a string
Creating Guid
from SHA256 hash seem like an easy option:
var guid = new Guid(
System.Security.Cryptography.SHA256.Create()
.ComputeHash(Encoding.UTF8.GetBytes("70c3bdc5ceeac673")).Take(16).ToArray());
Code discards half of hash result, but it does not change the fact that the same string is always transformed to the same Guid.
Alternatively depending on your requirements just converting string to byte array and padding with 0/removing extra may be enough.
Create a Guid from an int
If you create a Guid based on an integer it will no longer be a globally unique identifier.
Having said that you can use the constructor that takes an integer and some other parameters:
int a = 5;
Guid guid = new Guid(a, 0, 0, new byte[8]);
Not really advisable...
Related Topics
Using Dataannotations with Entity Framework
Filter SQL Based on C# List Instead of a Filter Table
Why Must We Define Both == and != in C#
Getting Downloads Folder in C#
How to Know the Size of the String in Bytes
How to Extend Identityuser with Custom Property
Get Number of Listeners, Clients Connected to Signalr Hub
Open Source Cad Drawing (Dwg) Library in C#
Capturing Mouse Events from Every Component
Processinfo and Redirectstandardoutput
C# MVC 3 Using Selectlist with Selected Value in View
Linq Where Ignore Accentuation and Case