C# how to create a Guid value?

C#Guid

C# Problem Overview


One field of our struct is Guid type. How to generate a valid value for it?

C# Solutions


Solution 1 - C#

Guid id = Guid.NewGuid();

Solution 2 - C#

Guid.NewGuid() creates a new random guid.

Solution 3 - C#

There are two ways

var guid = Guid.NewGuid();

or

var guid = Guid.NewGuid().ToString();

both use the Guid class, the first creates a Guid Object, the second a Guid string.

Solution 4 - C#

Guid.NewGuid() will create one

Solution 5 - C#

var guid = new Guid();

Hey, its a 'valid', although not very useful, Guid.

(the guid is all zeros, if you don't know. Sometimes this is needed to indicate no guid, in cases where you don't want to use a nullable Guid)

Solution 6 - C#

To makes an "empty" all-0 guid like 00000000-0000-0000-0000-000000000000.

var makeAllZeroGuID = new System.Guid();

or

var makeAllZeroGuID = System.Guid.Empty;

To makes an actual guid with a unique value, what you probably want.

var uniqueGuID = System.Guid.NewGuid(); 

Solution 7 - C#

If you want to create a "desired" Guid you can do

var tempGuid = Guid.Parse("<guidValue>");

where <guidValue> would be something like 1A3B944E-3632-467B-A53A-206305310BAE.

Solution 8 - C#

System.Guid desiredGuid = System.Guid.NewGuid();

Solution 9 - C#

There's also ShortGuid - A shorter and url friendly GUID class in C#. It's available as a Nuget. More information here.

PM> Install-Package CSharpVitamins.ShortGuid

Usage:

Guid guid = Guid.NewGuid();
ShortGuid sguid1 = guid; // implicitly cast the guid as a shortguid
Console.WriteLine(sguid1);
Console.WriteLine(sguid1.Guid);

This produces a new guid, uses that guid to create a ShortGuid, and displays the two equivalent values in the console. Results would be something along the lines of:

ShortGuid: FEx1sZbSD0ugmgMAF_RGHw 
Guid:      b1754c14-d296-4b0f-a09a-030017f4461f

Solution 10 - C#

If you are using this in the Reflection C#, you can get the guid from the property attribute as follows

var propertyAttributes= property.GetCustomAttributes();
foreach(var attribute in propertyAttributes)
{
  var myguid= Guid.Parse(attribute.Id.ToString());
}

Solution 11 - C#

It's really easy. The .Net framework provides an in-built function to create and parse GUIDS. This is available in the System namespace and the static Guid class.

To create a GUID just use the code below:

var newGuid = System.Guid.NewGuid(); 

To parse a GUID string as a GUID, use the code below:

var parsedGuid = System.Guid.Parse(guidString);

If you just want to create a new guide and just use it in your application, just use one of the online GUID Generator tools online to create yourself a new guid.

Solution 12 - C#

// Create and display the value of two GUIDs.
Guid g = Guid.NewGuid();
Console.WriteLine(g);
Console.WriteLine(Guid.NewGuid());

// This code example produces a result similar to the following:

// 0f8fad5b-d9cb-469f-a165-70867728950e
// 7c9e6679-7425-40de-944b-e07fc1f90ae7

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question5YrsLaterDBAView Question on Stackoverflow
Solution 1 - C#DavidView Answer on Stackoverflow
Solution 2 - C#Adam DriscollView Answer on Stackoverflow
Solution 3 - C#JustinView Answer on Stackoverflow
Solution 4 - C#TWAView Answer on Stackoverflow
Solution 5 - C#user1228View Answer on Stackoverflow
Solution 6 - C#reza.cse08View Answer on Stackoverflow
Solution 7 - C#ZeliaxView Answer on Stackoverflow
Solution 8 - C#siphabView Answer on Stackoverflow
Solution 9 - C#Joel WiklundView Answer on Stackoverflow
Solution 10 - C#SAliaMunchView Answer on Stackoverflow
Solution 11 - C#SameerView Answer on Stackoverflow
Solution 12 - C#luqman ahmadView Answer on Stackoverflow