One field of our struct is Guid type. How to generate a valid value for it?
 
    
    - 7,243
- 6
- 49
- 61
 
    
    - 33,370
- 43
- 136
- 210
- 
                    1Onine you can use this https://devtoolsonline20190908040816.azurewebsites.net/DevTools/GuidGenerator – GomuGomuNoRocket Sep 11 '19 at 11:45
12 Answers
Guid id = Guid.NewGuid();
 
    
    - 12,451
- 1
- 22
- 17
- 
                    332If 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
- 
                    16You 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
- 
                    14Little correction, there is no function 'toString()' on Guid object, it is 'ToString()' – A.B. Jun 02 '18 at 11:01
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.
 
    
    - 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
Guid.NewGuid() creates a new random guid.
 
    
    - 30,615
- 24
- 120
- 162
 
    
    - 9,395
- 9
- 61
- 104
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(); 
 
    
    - 30,615
- 24
- 120
- 162
 
    
    - 5,938
- 48
- 39
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)
- 
                    That does not create all zeros. It creates a valid Guid. Guid.Empty returns all zeros. – FlavorScape May 21 '13 at 18:28
- 
                    10@FlavorScape before you try, I bet you 50 rep (via bounty) you are wrong. Deal? – May 21 '13 at 18:48
- 
                    meh, i was thinking of Guid.NewGuid(). I really care to verify the new Guid() – FlavorScape May 21 '13 at 21:34
- 
                    11I beat you by one with this solution: `var guid = new Guid(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, });` – Jeppe Stig Nielsen Jul 14 '13 at 00:08
- 
                    @JeppeStigNielsen Never realized that one could create a guid from a byte array. And that's awesome because I'm in need to guidify my hashes! – Konrad Viltersten Aug 15 '16 at 20:34
- 
                    What cases can you provide where one would use this over a nullable guid? – ComeIn May 14 '19 at 03:36
- 
                    3When you must pass a guid to code you don't own that doesn't accept a nullable guid. – May 14 '19 at 13:07
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.
 
    
    - 4,987
- 10
- 51
- 79
- 
                    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
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
 
    
    - 1,697
- 2
- 18
- 24
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());
}
 
    
    - 67
- 9
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.
 
    
    - 383
- 1
- 10
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
 
    
    - 74
- 4
 
     
    