Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guid.Parse() or new Guid() - What's the difference?

Tags:

c#

.net

guid

What is the difference between these two ways of converting a string to System.Guid? Is there a reason to choose one over the other?

var myguid = Guid.Parse("9546482E-887A-4CAB-A403-AD9C326FFDA5"); 

or

var myguid = new Guid("9546482E-887A-4CAB-A403-AD9C326FFDA5"); 
like image 249
brennazoon Avatar asked Aug 02 '11 17:08

brennazoon


People also ask

What is GUID parse?

Parses a span of characters into a value. Parse(ReadOnlySpan<Char>) Converts a read-only character span that represents a GUID to the equivalent Guid structure. Parse(String) Converts the string representation of a GUID to the equivalent Guid structure.

What is new GUID?

Description. The New-Guid cmdlet creates a random globally unique identifier (GUID). If you need a unique ID in a script, you can create a GUID, as needed.

How do I generate a new GUID?

To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.

How do you check if a GUID is valid or not?

A span containing the characters representing the GUID to convert. When this method returns, contains the parsed value. If the method returns true , result contains a valid Guid. If the method returns false , result equals Empty.


1 Answers

A quick look in the Reflector reveals that both are pretty much equivalent.

public Guid(string g) {     if (g == null)     {        throw new ArgumentNullException("g");     }     this = Empty;     GuidResult result = new GuidResult();     result.Init(GuidParseThrowStyle.All);     if (!TryParseGuid(g, GuidStyles.Any, ref result))     {         throw result.GetGuidParseException();     }     this = result.parsedGuid; }  public static Guid Parse(string input) {     if (input == null)     {         throw new ArgumentNullException("input");     }     GuidResult result = new GuidResult();     result.Init(GuidParseThrowStyle.AllButOverflow);     if (!TryParseGuid(input, GuidStyles.Any, ref result))     {         throw result.GetGuidParseException();     }     return result.parsedGuid; } 
like image 71
Jakub Konecki Avatar answered Oct 16 '22 03:10

Jakub Konecki