613

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

John Smith
  • 7,243
  • 6
  • 49
  • 61
5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210

12 Answers12

1098
Guid id = Guid.NewGuid();
David
  • 12,451
  • 1
  • 22
  • 17
  • 332
    If you, like me, make the mistake of doing (new Guid().toString()) you will get 0000-00000-00000-00000. You need to do Guid.NewGuid().toString() – Joao Carlos Jan 03 '15 at 15:10
  • 16
    You might be interested in formatting Guid too https://msdn.microsoft.com/en-us/library/97af8hh4(v=vs.110).aspx – Anil Vangari Mar 22 '16 at 01:09
  • 14
    Little correction, there is no function 'toString()' on Guid object, it is 'ToString()' – A.B. Jun 02 '18 at 11:01
106

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.

Justin
  • 4,002
  • 2
  • 19
  • 21
  • 54
    @Justin, That's kind of one way to do it. `var guid = Guid.NewGuid().ToString()` just turns it in to a string. – Michael Meadows Feb 26 '10 at 19:10
  • 1
    @MichaelMeadows Yes that is correct, the first one creates a new Guid Object, the second creates a string. – Justin Sep 01 '15 at 16:38
105

Guid.NewGuid() creates a new random guid.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Adam Driscoll
  • 9,395
  • 9
  • 61
  • 104
61

Guid.NewGuid() will create one

TWA
  • 12,756
  • 13
  • 56
  • 92
37

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(); 
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
reza.cse08
  • 5,938
  • 48
  • 39
36
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)

32

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.

Zeliax
  • 4,987
  • 10
  • 51
  • 79
22
System.Guid desiredGuid = System.Guid.NewGuid();
Ahmad
  • 69,608
  • 17
  • 111
  • 137
siphab
  • 451
  • 4
  • 11
  • This seems to be the new way to do this, as of at least 2012. I don't seem to have Guid.NewGuid() available in 2015. – Dave Yarwood Aug 04 '15 at 20:55
  • 2
    @DaveYarwood Guid has been under the System namespace for a very long time, and is what every one else is referring to in the other answers (it just happens to be that a new class already has the Using added in for the System Namespace in the normal template) – Stephen Aug 23 '15 at 19:42
7

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
Joel Wiklund
  • 1,697
  • 2
  • 18
  • 24
1

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());
}


SAliaMunch
  • 67
  • 9
1

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.

Sameer
  • 383
  • 1
  • 10
0

If you are using the ABP framework, it is recommended to use IGuidGenerator.Create() instead of Guid.NewGuid(). This is to ensure GUIDs are sequential, improving read/write times on the database. https://docs.abp.io/en/abp/latest/Guid-Generation#iguidgenerator