In previous article, I mentioned how to Convert JSON to CSV in C# but in this article, I have mentioned what is GUID in C# and how we can create GUID in C# or generate GUID with various examples.
What is GUID in C#?
GUID represent a Globally unique identifier (GUID). A GUID is a 128-bit integer (16 bytes) that you can use across all computers and networks wherever a unique identifier is required.
Guids are most often used as primary keys in databases. Their advantage is that you don't have to call the database to get a new ID that is (almost) guaranteed to be unique.
System.GUID
class represents a GUID in .NET Framework.
Create or Generate GUID in C#
You can simply use Guid.NewGuid() to create new GUID in C#, here is the complete console application example, which generates GUID
using System;
public class Program
{
public static void Main()
{
Guid obj = Guid.NewGuid();
Console.WriteLine("Guid generated is " + obj.ToString());
}
}
Output:
If you will see above code and note, we are using Guid.NewGuid()
not new GUID()
, as we usually do while creating a class instance.
If you will use new Guid()
, it will always generate empty string (16 byte with all 0)
What is the string length of a GUID?
It depends on how you format the Guid:
- Guid.NewGuid().ToString() = 36 characters (Hyphenated), output -> 12345678-1234-1234-1234-123456789abc
- Guid.NewGuid().ToString("D") = 36 characters (Hyphenated, same as ToString()), outputs -> 12345678-1234-1234-1234-123456789abc
- Guid.NewGuid().ToString("N") = 32 characters (Digits only), outputs -> 12345678123412341234123456789abc
- Guid.NewGuid().ToString("B") = 38 characters (Braces), output -> outputs: {12345678-1234-1234-1234-123456789abc}
- Guid.NewGuid().ToString("P") = 38 characters (Parentheses), outputs-> (12345678-1234-1234-1234-123456789abc)
- Guid.NewGuid().ToString("X") = 68 characters (Hexadecimal), outputs -> {0x12345678,0x1234,0x1234{0x12,0x34,0x12,0x34,0x56,0x78,0x9a,0xbc}}
Generate GUID with specific length
You cannot do that using GUID.NewGUID() method, but you can create your own custom function (not recommended, as A GUID with a user-defined length is no longer a GUID.)
using System;
using System.Text;
public class Program
{
public static void Main()
{
Console.WriteLine(generateRandomStr(18));
}
public static string generateRandomStr(int length)
{
StringBuilder myGuidLikeString = new StringBuilder();
while(myGuidLikeString.Length < length)
{
myGuidLikeString.Append(Guid.NewGuid().ToString());
}
return myGuidLikeString.ToString(0,length);
}
}
Output:
740d0955-ae0d-4e11
OR
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
Random rn = new Random();
string charsToUse = "AzByCxDwEvFuGtHsIrJqKpLoMnNmOlPkQjRiShTgUfVeWdXcYbZa1234567890";
MatchEvaluator RandomChar = delegate (Match m)
{
return charsToUse[rn.Next( charsToUse.Length )].ToString();
};
Console.WriteLine( Regex.Replace( "XXXX-XXXX-XXXX-XXXX-XXXX", "X", RandomChar ) );
// 8J1P-fLcr-kwyt-4Mer-BDeT
}
}
Create New Empty GUID (all zero)
You can use Guid.Empty which will create
Example
var guid = Guid.Empty;
OR
You can also create GUID with byte array and check if it is empty, using below code
var bytes = new Byte[16];
var guid2 = new Guid(bytes);
Console.WriteLine(guid2); // 00000000-0000-0000-0000-000000000000
Console.WriteLine($"{guid2 == Guid.Empty}"); //true
That's it, hope it clears your concepts related to GUID in C#.
You may also like to read:
Get First Character or Nth Character of string in C#